Skip to content

Instantly share code, notes, and snippets.

@connrs
Created March 26, 2013 11:52
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 connrs/5244842 to your computer and use it in GitHub Desktop.
Save connrs/5244842 to your computer and use it in GitHub Desktop.
Callback delay with reset
// I just wanted a quick reference for this function as it's a common pattern
// used when managing DOM events that may fire multiple times in a short period
// of time.
function delayWithReset(delayMilliseconds, callback) {
var timeout = null;
var func = function() {
var args = Array.prototype.slice.apply(arguments);
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
callback.apply(null, args);
}, delayMilliseconds);
};
return func;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment