Skip to content

Instantly share code, notes, and snippets.

@sethkrasnianski
Created January 20, 2015 01:30
Show Gist options
  • Save sethkrasnianski/3016ce5a33af68766e4e to your computer and use it in GitHub Desktop.
Save sethkrasnianski/3016ce5a33af68766e4e to your computer and use it in GitHub Desktop.
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