34 lines
1.1 KiB
CoffeeScript
34 lines
1.1 KiB
CoffeeScript
# This is the same as setTimeout but then
|
|
# with switched arguments for coffeescript friendlyness.
|
|
# @a = 3
|
|
# delay =>
|
|
# console.log "The value of a is #{@a}"
|
|
# or to delay not a second but a self defined amount of time in milliseconds:
|
|
# delay 10000, =>
|
|
# console.log "The value of a is #{@a}"
|
|
@delay = (time = 1000, fn, args...) ->
|
|
setTimeout fn, time, args...
|
|
|
|
# based on http://www.thecodeship.com/web-development/alternative-to-javascript-evil-setinterval/
|
|
# can be used as:
|
|
# to keep scope and execute every second:
|
|
# @a = 3
|
|
# interval =>
|
|
# console.log "Hi there with a is: #{@a}"
|
|
# to log every 200 milliseconds without conservation of this scope:
|
|
# interval 200, -> console.log "Hi there"
|
|
# to execute a maximum number (30) of times:
|
|
# interval 200, 30, -> console.log "Hi there"
|
|
@interval = (wait = 1000, extra_args..., func)->
|
|
times = extra_args[0]
|
|
interv = do (wait, times)->
|
|
->
|
|
return if times? and times-- <= 0
|
|
setTimeout interv, wait
|
|
try
|
|
func.call null
|
|
catch e
|
|
times = 0
|
|
throw e.toString()
|
|
setTimeout interv, wait
|