-
Notifications
You must be signed in to change notification settings - Fork 1
/
debounce.js
34 lines (33 loc) · 816 Bytes
/
debounce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function debounce(fn, delay) {
let timer = null;
return function () {
let context = this;
let args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
function opDebounce(fn, delay, options) {
var timer = null,
first = true,
leading;
if (typeof options === 'object') {
leading = !!options.leading;
}
return function () {
let context = this,
args = arguments;
if (first && leading) {
fn.apply(context, args);
first = false;
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}