Skip to content

Instantly share code, notes, and snippets.

@kanakiyajay
Created September 16, 2014 13:14
Show Gist options
  • Save kanakiyajay/9f89253fb1d3cdae00db to your computer and use it in GitHub Desktop.
Save kanakiyajay/9f89253fb1d3cdae00db to your computer and use it in GitHub Desktop.
Throttle: Call a function only once every few milliseconds. Important for scroll events, or when a function is called multiple times.
function throttle(delay, callback) {
var previousTime = new Date().getTime();
return function() {
var newTime = new Date().getTime();
if ((newTime - previousTime) > delay) {
previousTime = newTime;
callback.apply(null, arguments);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment