Skip to content

Instantly share code, notes, and snippets.

@desandro
Created January 22, 2014 14:13
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 desandro/8559356 to your computer and use it in GitHub Desktop.
Save desandro/8559356 to your computer and use it in GitHub Desktop.
// Original by John Hann
// http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
function debounce( fn, threshold ) {
var timeout;
return function debounced() {
var _this = this;
var args = arguments;
if ( timeout ) {
clearTimeout( timeout );
}
timeout = setTimeout( function delayed() {
fn.apply( _this, args );
timeout = null;
}, threshold || 100 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment