Created
June 7, 2011 01:13
-
-
Save mkuklis/1011477 to your computer and use it in GitHub Desktop.
JavaScript debounce
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
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