You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
615 B
34 lines
615 B
module.exports = debounce;
|
|
|
|
function debounce(fn, delay, atStart, guarantee) {
|
|
var timeout;
|
|
var args;
|
|
var self;
|
|
|
|
return function debounced() {
|
|
self = this;
|
|
args = Array.prototype.slice.call(arguments);
|
|
|
|
if (timeout && (atStart || guarantee)) {
|
|
return;
|
|
} else if (!atStart) {
|
|
clear();
|
|
|
|
timeout = setTimeout(run, delay);
|
|
return timeout;
|
|
}
|
|
|
|
timeout = setTimeout(clear, delay);
|
|
fn.apply(self, args);
|
|
|
|
function run() {
|
|
clear();
|
|
fn.apply(self, args);
|
|
}
|
|
|
|
function clear() {
|
|
clearTimeout(timeout);
|
|
timeout = null;
|
|
}
|
|
};
|
|
}
|
|
|