Created
March 27, 2015 22:23
-
-
Save damirm/dabf9da64ab4e9c94c79 to your computer and use it in GitHub Desktop.
Wait for seconds
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
var waitSeconds = function (seconds) { | |
if (waitSeconds.promise) { | |
return waitSeconds.promise; | |
} | |
var defer = $.Deferred(); | |
setTimeout(function () { | |
defer.resolve(); | |
}, 1000 * seconds); | |
var promise = defer.promise(); | |
waitSeconds.promise = promise; | |
return promise; | |
}; | |
/** | |
* usage: | |
* var fiveSeconds = waitSeconds(5); | |
* fiveSeconds.then(function () { | |
* console.log('5 seconds left'); | |
* }); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment