Skip to content

Instantly share code, notes, and snippets.

@darrylhebbes
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save darrylhebbes/e8dc0e65f87a7b51a75d to your computer and use it in GitHub Desktop.

Select an option

Save darrylhebbes/e8dc0e65f87a7b51a75d to your computer and use it in GitHub Desktop.
Throttle Function
function callback () {
console.count("Throttled");
}
window.addEventListener("resize", throttle( callback, 300 ));
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment