Skip to content

Instantly share code, notes, and snippets.

@Jontyy
Created December 15, 2011 22:22
Show Gist options
  • Save Jontyy/1483197 to your computer and use it in GitHub Desktop.
Save Jontyy/1483197 to your computer and use it in GitHub Desktop.
JavaScript Debounce Function
//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