Created
September 2, 2017 01:29
-
-
Save patmigliaccio/29cd7da159fcb9cf46a4415ad613001c to your computer and use it in GitHub Desktop.
patmigliaccio.com/rate-limiting 3/20/2017
This file contains 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 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