Skip to content

Instantly share code, notes, and snippets.

@pl12133
Created January 22, 2019 20:34
Show Gist options
  • Save pl12133/86fe9f296d2898117062ef5233308877 to your computer and use it in GitHub Desktop.
Save pl12133/86fe9f296d2898117062ef5233308877 to your computer and use it in GitHub Desktop.
function leakyDebounce(fn, delay, max = 1) {
let timer, ctx, args, outstanding = 0, queue = [];
function done() {
[ ctx, args ] = queue.shift();
fn.apply(ctx, args);
if (queue.length) {
timer = setTimeout(done, delay);
}
}
return function(...innerArgs) {
if (queue.length >= max) { return; }
queue.push([ this, args ]);
clearTimeout(timer);
timer = setTimeout(done, delay);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment