這是代碼:setTimeout在地圖內部不執行?或setTimeOut在地圖功能?
while(countLoop < count) {
let randIndex = Math.floor(Math.random()*4); // returns // 1 to 3 decimal, this will be used for colors indexes
console.log("while true count = ",randIndex)
this.setState(
({colorsChallengeForUser}, props) => ({
colorsChallengeForUser: [...colorsChallengeForUser, randIndex]
}),
() => { // setState has a default callback we make use of that here.
let { colorsChallengeForUser } = this.state;
colorsChallengeForUser.map((item, index, array) => {
switch(item) {
case 0:
// red.play()
setTimeout(red.play(), 3000);
break;
case 1:
// green.play()
setTimeout(green.play(), 3000);
break;
case 2:
// yellow.play()
setTimeout(yellow.play(), 3000);
break;
case 3:
// yellow.play() // this wo
setTimeout(blue.play(), 3000);
break;
defalt:
console.error(`Unknown ${item}`);
}
});
}
);
countLoop++;
}
所有的作品,但設置超時不能正常工作,他們都在當JS評價同時播放。如何使用setTimeOut使地圖執行速度變慢?
'的setTimeout;'立即運行...'的setTimeout(red.play,3000) ;'在超時到期時執行'red.play()'...你爲什麼在回調函數沒有返回任何東西時使用'.map'?使用'.forEach'代替 –
除非'red.play()'返回一個函數,否則你應該遵循@JaromandaX提示。 – Redu
是真的(不太可能) –