Created
May 15, 2014 17:49
-
-
Save ReedD/56d9dddf9d7a68d056b8 to your computer and use it in GitHub Desktop.
A function wrapper to repeatedly delay a given JavaScript function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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