Skip to content

Instantly share code, notes, and snippets.

@patmigliaccio
Created September 2, 2017 01:29
Show Gist options
  • Save patmigliaccio/29cd7da159fcb9cf46a4415ad613001c to your computer and use it in GitHub Desktop.
Save patmigliaccio/29cd7da159fcb9cf46a4415ad613001c to your computer and use it in GitHub Desktop.
patmigliaccio.com/rate-limiting 3/20/2017
function limiter(fn, wait){
let isCalled = false,
calls = [];
let caller = function(){
if (calls.length && !isCalled){
isCalled = true;
calls.shift().call();
setTimeout(function(){
isCalled = false;
caller();
}, wait);
}
};
return function(){
calls.push(fn);
caller();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment