Skip to content

Instantly share code, notes, and snippets.

@crstauf
Last active October 18, 2018 15:10
Show Gist options
  • Save crstauf/80f8f213cc19eab75f031b1d440b34fd to your computer and use it in GitHub Desktop.
Save crstauf/80f8f213cc19eab75f031b1d440b34fd to your computer and use it in GitHub Desktop.
/**
* Source: https://remysharp.com/2010/07/21/throttling-function-calls
*/
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 = +new Date;
fn.apply(context, args);
}, threshhold + ( last - now ));
} else {
last = now;
fn.apply(context, args);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment