Throttle function
by jorgeluis
HTML
Throttle function, used to add a rate limiter to any existing function.
http://sampsonblog.com/749/simple-throttle-function
JavaScript
function throttle (callback, limit) {
var wait = false; // Initially, we're not waiting
return function () { // We return a throttled function
if (!wait) { // If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () { // After a period of time
wait = false; // And allow future invocations
}, limit);
}
}
}
// demonstrate
function callback () {
console.count("Throttled");
}
window.addEventListener("resize", throttle( callback, 200 ));