Skip to content

Instantly share code, notes, and snippets.

@straps
Created November 3, 2010 11:50
Show Gist options
  • Save straps/661000 to your computer and use it in GitHub Desktop.
Save straps/661000 to your computer and use it in GitHub Desktop.
debounce
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 || 100);
};
}
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);
};
}
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