1
我已經列出了一個我希望一個接一個執行的操作。 比方說,我們有一個拳擊袋鍛鍊:在它們之間運行延遲操作
嘟嘟的聲音,然後在2秒後教練告訴運動員該做什麼('FOOTWORK')。 15秒後,教練告訴運動員改變他正在做的事('技巧')......這一直持續到一分鐘過去。然後,教師重複此過程3次。
我想建立一些圖書館,但確實如此,但我遇到了每個動作之間的延遲問題。以下是我迄今所做的:
class Action{
constructor(name = "Action", actualAction){
this.name = name;
this.actualAction = actualAction;
}
run(){
console.log("Executing Action: " + this.name);
this.actualAction();
}
}
function repeat(times){
var timesLeft = times;
return function(){
timesLeft--;
return timesLeft > 0;
}
}
class SleepAction extends Action{
constructor(ms, nextAction){
super("Sleep " + ms);
this.ms = ms;
this.nextAction = nextAction;
}
run(){
setTimeout(this.nextAction.run(), this.ms);
}
}
class Block extends Action{
constructor(name = "Block", actions, repeat){
super(name);
this.repeat = repeat;
this.instructions = actions;
}
run(){
this.instructions.forEach(function(action) {
action.run();
});
if(this.repeat()){
this.run();
}
}
}
你可以告訴我使用的setTimeout,試圖得到這個工作,但所有的行動在這個例子中同時運行:
var doNothing = new Action("Nothing", function(){});
var boxingBagPreset = new Block("Boxing Bag 15-15-15-15 3 Times",
[beepAction,
new SleepAction(2000, new Block("Tiny Pause", [
new Action("FOOTWORK", textToSpeech("FOOTWORK")),
new SleepAction(15000, new Block("Sleep 15", [
new Action("SPEED", textToSpeech("SPEED")),
new SleepAction(15000, new Block("Sleep 15", [
new Action("POWER", textToSpeech("POWER")),
new SleepAction(15000, new Block("Sleep 15", [
new Action("REST", textToSpeech("REST")),
new SleepAction(15000, new Block("Sleep 15", [doNothing], repeat(1)))
], repeat(1)))
], repeat(1)))
] , repeat(1)))
], repeat(1)))],
repeat(3));
爲了實現這個目標,我需要改變什麼?
好的,謝謝它解決了這個問題,除了repeat()部分。你有什麼建議,除了擴大連鎖3次,以使其工作? (它像BEEP-BEEP-BEEP * 15s睡覺* TECH-TECH-TECH ...等 – Zarkopafilis
@Zarkopafilis我不明白,但也許你可以創建一個新的問題和詳細說明。 – styfle