Skip to content

Instantly share code, notes, and snippets.

@tjmw
Created September 4, 2015 20:28
Show Gist options
  • Save tjmw/2b4e472b6cba112fb77c to your computer and use it in GitHub Desktop.
Save tjmw/2b4e472b6cba112fb77c to your computer and use it in GitHub Desktop.
Javascript Debouncer
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