Created
September 2, 2017 01:27
-
-
Save patmigliaccio/4c6e625b0fbe826f431bd2564dda0f54 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
| // Broken Code | |
| function limiter(fn, wait){ | |
| let isCalled = false, | |
| calls = []; | |
| return function(){ | |
| calls.push(fn); | |
| // Infinite Loop | |
| while (calls.length){ | |
| if (!isCalled){ | |
| calls.shift().call(); | |
| isCalled = true; | |
| setTimeout(function(){ | |
| isCalled = false; | |
| }, wait) | |
| } | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment