Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Created February 7, 2014 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colingourlay/8857692 to your computer and use it in GitHub Desktop.
Save colingourlay/8857692 to your computer and use it in GitHub Desktop.
Debounce
function debounce(fn, wait) {
var timeout;
return function() {
var context = this, // preserve context
args = arguments, // preserve arguments
later = function() { // define a function that:
timeout = null; // * nulls the timeout (GC)
fn.apply(context, args); // * calls the original fn
};
// (re)set the timer which delays the function call
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment