Created
December 7, 2012 13:05
-
-
Save JamieMason/4233170 to your computer and use it in GitHub Desktop.
until(fn:Function, delay:Number):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
/** | |
* Returns a function which won't be invoked until delay has elapsed, without further calls being made during that interval. | |
* If further calls are made, the delay is reset and the cycle repeats. | |
* @param {Function} fn Behaviour to be wrapped in this delay | |
* @param {Number} delay Duration in milliseconds | |
* @return {Function} | |
*/ | |
function until(fn, delay) { | |
var timeoutId; | |
var scheduledCall = setTimeout.bind(window, fn, delay); | |
return function() { | |
clearTimeout(timeoutId); | |
timeoutId = scheduledCall.apply(this, arguments); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
... 2 seconds pass