Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Created June 4, 2016 14:41
Show Gist options
  • Save bhavyaw/0c870c3b96c48e75ff380b2c39564187 to your computer and use it in GitHub Desktop.
Save bhavyaw/0c870c3b96c48e75ff380b2c39564187 to your computer and use it in GitHub Desktop.
Javascript Debounce Utilities
// 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