Skip to content

Instantly share code, notes, and snippets.

@ReedD
Created May 15, 2014 17:49
Show Gist options
  • Save ReedD/56d9dddf9d7a68d056b8 to your computer and use it in GitHub Desktop.
Save ReedD/56d9dddf9d7a68d056b8 to your computer and use it in GitHub Desktop.
A function wrapper to repeatedly delay a given JavaScript function
/**
* A function wrapper to repeatedly delay a given function
* Example:
*
* // Generic usage
* delay(function () {
* console.log('1 second delay');
* }, 1000);
*
* // Namespaced delay
* delay(function () {
* console.log('1 second delay');
* }, 1000, 'log');
*
*/
var delay = (function() {
var timer = [];
return function(callback, ms, key) {
key = key || 0;
clearTimeout(timer[key]);
timer[key] = setTimeout(callback, ms);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment