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