Last active
August 29, 2015 14:06
-
-
Save codyromano/db6f680d662329bbf7c2 to your computer and use it in GitHub Desktop.
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
<script> | |
var rateLimit = (function () { | |
'use strict'; | |
var processes = {}; | |
return function (pID, fn, rateLimit) { | |
var process = processes[pID]; | |
if (!process) { | |
process = processes[pID] = {stack: [], polling: false, time: null}; | |
} | |
process.stack.push(fn); | |
if (!process.polling) { | |
(function pollingCycle () { | |
var nowTime = (new Date).getTime(); | |
if (process.stack.length == 0) { | |
process.polling = false; | |
return false; | |
} | |
if (!process.time || nowTime - process.time >= rateLimit) { | |
process.time = nowTime; | |
process.stack[0](); | |
process.stack.slice(0,1); | |
} else { | |
setTimeout(pollingCycle, 100); | |
} | |
})(); | |
} | |
}; | |
})(); | |
var i = 0; | |
while (++i < 3) { | |
rateLimit('annoyingAlert', function () { | |
alert("Fortunately, these annoying alerts are rate limited."); | |
}, 2000); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment