Skip to content

Instantly share code, notes, and snippets.

@zhoufenfens
Created December 11, 2014 03:47
Show Gist options
  • Save zhoufenfens/268a4b62d9b05e686622 to your computer and use it in GitHub Desktop.
Save zhoufenfens/268a4b62d9b05e686622 to your computer and use it in GitHub Desktop.
var throttle = function(func, wait) {
var context, args, timeout, result, previous, later;
previous = 0;
later = function() {
previous = new Date();
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date(), 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;
};
}
@zhoufenfens
Copy link
Author

  function throttle(){}
first second
1 2
3 4

@zhoufenfens
Copy link
Author

🎎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment