Created
December 15, 2011 22:22
-
-
Save Jontyy/1483197 to your computer and use it in GitHub Desktop.
JavaScript Debounce Function
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
//usage: http://jsbin.com/ucucuc | |
var debounce = function(delay,callback){ | |
var timeout; | |
//$().keyup(debounce()) will be this function, it still has access to timeout because of closure | |
return function(){ | |
var that = this,args = arguments, | |
delayed = function(){ | |
callback.apply(that, args); | |
timeout = null; | |
}; | |
//if we have already have something queued then clear it | |
timeout && clearTimeout(timeout); | |
//each time this is called set the timeout to something else | |
timeout = setTimeout(delayed, delay || 250); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment