Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created September 12, 2011 01:23
Show Gist options
  • Save rjungemann/1210409 to your computer and use it in GitHub Desktop.
Save rjungemann/1210409 to your computer and use it in GitHub Desktop.
Enforce a timeout in CoffeeScript
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