繰り返し処理の登録と実行
funcで登録した関数をinterval毎に実行し続ける感じ
ひとつの関数を登録するときはこう
repeat = Repeat(
interval: 100,
func: ->
console.log 'hoge'
)
# 実行
repeat.run()
# 止める
repeat.stop()
いくつも登録したいときはこう
repeat = Repeat(
interval: 100,
func:
key1: ->
console.log 'key1'
key2: ->
console.log 'key2'
)
# 実行
repeat.run('key1')
# 止める
repeat.stop()
Repeat
# Repeat func
#
# @param {}
class Repeat
constructor: (conf) ->
if not (@ instanceof Repeat)
return new Repeat(conf)
return @init(conf)
init: (conf) ->
@disable = no
@running = no
@conf =
interval: 50,
func: ->
for c of conf
@conf[c] = conf[c]
return
run: (func_key) ->
@disable = no
@running = yes
conf = @conf
do =>
if @disable
return
if typeof conf.func is 'function'
conf.func()
else if typeof conf.func is 'object'
conf.func[func_key]()
setTimeout(arguments.callee, conf.interval)
return
return
stop: ->
@disable = yes
@running = no
return
別タスクにできたほうが使い勝手よさそうかな・・・
ずどさんの影響でCoffeeScript始めました