Skip to content

Instantly share code, notes, and snippets.

@oboshto
Created January 12, 2017 13:26
Show Gist options
  • Save oboshto/ad43eb606acdc133aa773fc5cf4e64b1 to your computer and use it in GitHub Desktop.
Save oboshto/ad43eb606acdc133aa773fc5cf4e64b1 to your computer and use it in GitHub Desktop.
Debounce function for events
// debounce function that will wrap our event
function debounce(fn, delay) {
// maintain a timer
let timer = null;
// closure function that has access to timer
return function() {
// get the scope and parameters of the function
// via 'this' and 'arguments'
let context = this;
let args = arguments;
// if event is called, clear the timer and start over
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment