Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active December 31, 2015 03:59
Show Gist options
  • Save sebnilsson/7931165 to your computer and use it in GitHub Desktop.
Save sebnilsson/7931165 to your computer and use it in GitHub Desktop.
Convert function to throttle function
function throttleFn(fn, wait, context) {
wait = wait || defaultThrottleWait;
var last, timeoutId;
return function() {
var ctx = (context || this),
now = +new Date,
args = arguments;
if (last && now < (last + wait)) {
clearTimeout(timeoutId);
timeoutId = setTimeout(function() {
last = now;
}, wait);
} else {
last = now;
fn.apply(ctx, args);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment