Skip to content

Instantly share code, notes, and snippets.

@chrisvariety
Created January 6, 2014 21:04
Show Gist options
  • Save chrisvariety/8289807 to your computer and use it in GitHub Desktop.
Save chrisvariety/8289807 to your computer and use it in GitHub Desktop.
// from underscore.js
var throttle = function(func, wait) {
var context, args, timeout, result;
var previous = 0;
var later = function() {
previous = new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment