Created
January 22, 2019 20:34
-
-
Save pl12133/86fe9f296d2898117062ef5233308877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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