Skip to content

Instantly share code, notes, and snippets.

@patmigliaccio
Created September 2, 2017 01:30
Show Gist options
  • Save patmigliaccio/ed4de6a0304958d7d71a96b3f43836be to your computer and use it in GitHub Desktop.
Save patmigliaccio/ed4de6a0304958d7d71a96b3f43836be 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.bind(this, ...arguments));
// let args = Array.prototype.slice.call(arguments);
// calls.push(fn.bind.apply(fn, [this].concat(args)));
caller();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment