Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created April 25, 2011 08:52
Show Gist options
  • Save boopathi/940291 to your computer and use it in GitHub Desktop.
Save boopathi/940291 to your computer and use it in GitHub Desktop.
Javascript Debounce
/**
* @author Boopathi
* @description Debounce a function call.
* @usage function(){}.debounce()
*/
Function.prototype.debounce = function(threshold, execAsap) {
var func = this, timeout;
return function debounced(){
var obj= this, args = arguments;
function delayed() {
if(!execAsap)
func.apply(obj,args);
timeout = null;
}
if(timeout)
clearTimeout(timeout);
else if(execAsap)
func.apply(obj,args);
timeout = setTimeout(delayed, threshold || 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment