Skip to content

Instantly share code, notes, and snippets.

@boopathi
Forked from mkuklis/gist:1011477
Created June 11, 2011 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/1020875 to your computer and use it in GitHub Desktop.
Save boopathi/1020875 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