Created
January 12, 2017 13:26
-
-
Save oboshto/ad43eb606acdc133aa773fc5cf4e64b1 to your computer and use it in GitHub Desktop.
Debounce function for events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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