Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active January 13, 2017 18:50
Show Gist options
  • Save andreasvirkus/7aea1c59cdc87736117e7e6468cb7659 to your computer and use it in GitHub Desktop.
Save andreasvirkus/7aea1c59cdc87736117e7e6468cb7659 to your computer and use it in GitHub Desktop.
// Another poor man's vanilla throttle script
window.addEventListener('scroll', throttle(callback, 1000));
// v1
function throttle(fn, wait) {
var time = Date.now();
return function() {
if (time + wait - Date.now() < 0) {
fn();
time = Date.now();
}
};
}
// v2
function throttleTwo(callback, delay) {
var timeout;
return function() {
var context = this,
args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
callback.apply(context, args);
}, delay);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment