Skip to content

Instantly share code, notes, and snippets.

@kirstein
Created May 30, 2013 19:03
Show Gist options
  • Select an option

  • Save kirstein/5680273 to your computer and use it in GitHub Desktop.

Select an option

Save kirstein/5680273 to your computer and use it in GitHub Desktop.
Throttle events
function throttle(callback, throttleTimeout, throttleTicks) {
throttleTimeout = throttleTimeout || 50;
throttleTicks = throttleTicks || 20;
var tick = 0,
timeout;
return function() {
var args = arguments;
tick = (tick + 1) % throttleTicks;
clearTimeout(timeout);
if (tick === 0) {
callback.apply(null, arguments);
} else {
timeout = setTimeout(function() {
callback.apply(null, args);
}, throttleTimeout);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment