ループ処理を休み休み進めていくクラス
最近 SuperCollider のサンプルコードを読んだりしているのだけど、こういう感じのコードがあった。
Task({ var list = ["C", "D", "E", "F", "G", "A", "B"]; 50.do({ arg count; // 50回繰り返す (list.wrapAt(count) + " play").postln; 1.wait; // 1秒待つ (list.wrapAt(count) + " pause").postln; 0.25.wait; // 0.25秒待つ // たまにシャッフルする if (0.25.coin, { list = list.scramble; }) }) }).start
繰り返し処理を休み休み進めていくのだけど、音楽用途だとタイミングの制御と反復みたいな感じで結構便利そう。で、JavaScript でも似たことをしたいと思ってクラス化した。
https://gist.github.com/mohayonao/5485992
こういう感じで使える
t = new Task do:5, init: -> # startしたときに1回呼ばれる. 戻り値が args として渡される word:"yes", list:["ok", "good", "awesome", "cool"] , (count, args)-> console.log "#{count}: #{args.word}?" @wait 250 # 250ミリ秒待つ , (count, args)-> console.log "#{count}: #{args.word}..." setTimeout => @resume() , Math.random() * 2500 + 100 @wait Infinity # resumeされるまで待つ , (count, args)-> console.log "#{count}: #{args.word}!!" args.word = args.list[(Math.random() * args.list.length)|0] @wait 1000 # 1秒待つ t.start().onEnd = -> console.log 'end'