Skip to content

Instantly share code, notes, and snippets.

@maoosi
Last active May 9, 2016 02:35
Show Gist options
  • Save maoosi/f5efdf705a07d57553db to your computer and use it in GitHub Desktop.
Save maoosi/f5efdf705a07d57553db to your computer and use it in GitHub Desktop.
Simple JavaScript throttle function - From Grafikart https://www.grafikart.fr/tutoriels/javascript/debounce-throttle-642
function throttle(callback, delay) {
var last;
var timer;
return function () {
var context = this;
var now = +new Date();
var args = arguments;
if (last && now < last + delay) {
clearTimeout(timer);
timer = setTimeout(function () {
last = now;
callback.apply(context, args);
}, delay);
} else {
last = now;
callback.apply(context, args);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment