Skip to content

Instantly share code, notes, and snippets.

@airton
Last active May 16, 2018 22:44
Show Gist options
  • Save airton/05befd02380442a18a578f8f3945cf44 to your computer and use it in GitHub Desktop.
Save airton/05befd02380442a18a578f8f3945cf44 to your computer and use it in GitHub Desktop.
function throttle( func, limit ){
var inThrottle;
var lastFunc;
var lastRan;
return function() {
var context = this;
var args = arguments;
if (!inThrottle) {
func.apply(context, args);
lastRan = Date.now();
inThrottle = true;
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(function() {
if ((Date.now() - lastRan) >= limit) {
func.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment