Created
June 4, 2016 14:41
-
-
Save bhavyaw/0c870c3b96c48e75ff380b2c39564187 to your computer and use it in GitHub Desktop.
Javascript Debounce Utilities
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
// Implementation | |
// run key up event handler as debounced key down handlers | |
var | |
_debounceTimeout; // key debouncer required params | |
// Usage | |
debounceStart(function(){ | |
// Function which is to be debounced | |
}); | |
/** | |
* Debounce Start Function | |
* Only starting call will be executed ( in case successive function calls) | |
* and rest of them will be ignored | |
* | |
* wait depends on different event listeners --> for keydown event it works at around 450ms | |
*/ | |
function debounceStart(func){ | |
var callNow = !_debounceTimeout; | |
clearTimeout(_debounceTimeout); | |
_debounceTimeout = setTimeout(function(){ | |
_debounceTimeout = null; | |
},450); //blank timer | |
if(callNow) | |
func.call(undefined); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment