Skip to content

Instantly share code, notes, and snippets.

@elkorn
Created November 5, 2013 07:11
Show Gist options
  • Save elkorn/7315086 to your computer and use it in GitHub Desktop.
Save elkorn/7315086 to your computer and use it in GitHub Desktop.
Function debounce by Unscriptable
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