Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Created April 28, 2015 17:15
Show Gist options
  • Save sagiavinash/303a2f1153739de8859b to your computer and use it in GitHub Desktop.
Save sagiavinash/303a2f1153739de8859b to your computer and use it in GitHub Desktop.
Throttle Snippet - Plain JS
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
window.addEventListener('resize', throttle(function() {
// All the taxing stuff you do
}, 250));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment