Skip to content

Instantly share code, notes, and snippets.

@hashchange
Last active June 28, 2016 15:31
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 hashchange/d1ddb2da215aa194519a10119273fe69 to your computer and use it in GitHub Desktop.
Save hashchange/d1ddb2da215aa194519a10119273fe69 to your computer and use it in GitHub Desktop.
Stand-alone, straightforward debounce function. Executes the callback at the end (no option for executing at start).
function debounce( callback, delay, context ) {
var timer;
return function () {
var _this = context || this,
args = arguments;
// Clear a timer which has been created earlier in the sequence of calls.
timer && clearTimeout( timer );
// (Re-)start the timer, ie create a new one.
timer = setTimeout( function () {
callback.apply( _this, args );
}, delay );
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment