[英]Vue.js Countdown Timer occasionally skips
I have a vue.js countdown timer as follows:我有一个 vue.js 倒数计时器,如下所示:
countdownTimer() {
// exit method if it is active
if(this.isCountdownActive == true && this.gameState.round === round) return;
// first time set true
this.isCountdownActive = true
this.countdown = 10;
var downloadTimer = setInterval(() => {
if(this.countdown <= 0){
clearInterval(downloadTimer);
if (this.thisUser.captain) {
console.log("submit turn end")
Store.submitTurnEnd();
this.countdown = 10
}
// On exit interval, restart to false
this.isCountdownActive = false
}
this.countdown -= 1
}, 1000);
},
I would say about 75% of the time it works perfectly, however, the other 25% of the time it glitches so that it submits the turn either twice or right away.我会说大约 75% 的时间它工作得很好,但是,另外 25% 的时间它会出现故障,以便它两次或立即提交转牌。 How can I make sure that doesn't happen.
我怎样才能确保不会发生这种情况。 I would like to make sure it only submits once and clears the interval asap.
我想确保它只提交一次并尽快清除间隔。
Thanks for the help.谢谢您的帮助。
You most likely are calling the method multiple times.您很可能多次调用该方法。
Your early bailout condition您的早期救助条件
if(this.isCountdownActive == true && this.gameState.round === round) return;
You rely on this.gameState.round
to be equal to round
, if that's not the case you will register the countdown multiple times.您依靠
this.gameState.round
等于round
,如果不是这种情况,您将多次注册倒计时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.