Created
September 12, 2011 01:23
-
-
Save rjungemann/1210409 to your computer and use it in GitHub Desktop.
Enforce a timeout in CoffeeScript
This file contains hidden or 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
timeout = (interval, callback) -> | |
setTimeout callback, interval | |
interval = (interval, callback) -> | |
setInterval callback, interval | |
enforce = (interval, options) -> | |
t = timeout interval, -> | |
options.onfail() if options.onfail? | |
done = -> | |
clearTimeout t | |
options.oncomplete() if options.oncomplete? | |
options.onstart done if options.onstart? | |
enforce 1000, { | |
onstart: (done) -> | |
timeout 500, -> | |
done() | |
oncomplete: -> | |
console.log 'success' | |
onfail: -> | |
console.log 'failure' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment