Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery - Debounce events
var deBouncer = function($,cf,of, interval){
// deBouncer by hnldesign.nl
// more info: http://www.hnldesign.nl/work/code/debouncing-events-with-jquery/
//
// based on code by Paul Irish and the original debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
var debounce = function (func, threshold, execAsap) {
var 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 || interval);
};
};
jQuery.fn[cf] = function(fn){ return fn ? this.bind(of, debounce(fn)) : this.trigger(cf); };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment