Skip to content

Instantly share code, notes, and snippets.

@tingwei628
Created April 19, 2017 23:34
Show Gist options
  • Save tingwei628/4ad277afebcdfea80814304d12674b20 to your computer and use it in GitHub Desktop.
Save tingwei628/4ad277afebcdfea80814304d12674b20 to your computer and use it in GitHub Desktop.
/*
This code is from
https://remysharp.com/2010/07/21/throttling-function-calls/#comment-497362
*/
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);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment