Skip to content

Instantly share code, notes, and snippets.

@codyromano
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyromano/db6f680d662329bbf7c2 to your computer and use it in GitHub Desktop.
Save codyromano/db6f680d662329bbf7c2 to your computer and use it in GitHub Desktop.
<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