Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created June 7, 2011 01:13
Show Gist options
  • Save mkuklis/1011477 to your computer and use it in GitHub Desktop.
Save mkuklis/1011477 to your computer and use it in GitHub Desktop.
JavaScript debounce
function debounce(fn, wait) {
var timeout = null;
return function () {
clearTimeout(timeout);
var args = arguments;
var ctx = this;
timeout = setTimeout(function () {
fn.apply(ctx, args);
}, wait);
}
}
function print(value) {
console.log(value);
}
var printD = debounce(print, 100);
printD(1);
printD(2);
printD(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment