Created
September 4, 2015 20:28
-
-
Save tjmw/2b4e472b6cba112fb77c to your computer and use it in GitHub Desktop.
Javascript Debouncer
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
var debounce = function(fn) { | |
var timeout; | |
var debouncedFn = function() { | |
clearTimeout(timeout); | |
timeout = setTimeout(fn.apply.bind(fn, this, arguments), 500); | |
}; | |
return debouncedFn; | |
}; | |
var myFunction = function(name) { | |
console.log('Hi ' + name + '!'); | |
}; | |
var myDebouncedFunction = debounce(myFunction); | |
myDebouncedFunction('one'); | |
myDebouncedFunction('two'); | |
myDebouncedFunction('three'); | |
setTimeout(myDebouncedFunction.bind(this, 'four'), 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment