vue循环遍历定时器
需要每隔1秒遍历如下代码,目前的问题是隔1秒后全部执行,请问怎么改进
startReview(){
console.log('start')
for(let i=0; i<this.handHistory.actions.preflop.length; i++){
setTimeout(()=>{
var currentSeat = this.handHistory.actions.preflop[i].seat
var bet = this.handHistory.actions.preflop[i].chip
var action = this.handHistory.actions.preflop[i].action
this.player[currentSeat].currentBet = bet
this.player[currentSeat].actionStatus = action
},1000)
}
}
1000 改成 1000*(i+1),或者
startReview(){
console.log('start')
let i = 0;
const timer = setInterval(()=>{
if(i<this.handHistory.actions.preflop.length){
var currentSeat = this.handHistory.actions.preflop[i].seat
var bet = this.handHistory.actions.preflop[i].chip
var action = this.handHistory.actions.preflop[i].action
this.player[currentSeat].currentBet = bet
this.player[currentSeat].actionStatus = action
i++
}else{
clearInterval(timer)
}
},1000)
}
加上一些判断,可以让代码有选择地执行某一部分。
改成同步或者把函数拆出来
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!