每次我调用setInterval方法时,我的计数器都会加速-VUEJS
我正在开发一个函数调用的倒计时。目标是每次用户回答游戏中的问题时重新启动倒计时,下一个问题是实例化的。我一直面临的问题是,每次我在时间耗尽之前调用功能时,下一个倒计时会加速并跳过数字。
<template>
<div>
{{ countDown }}
</div>
</template>
<script>
export default {
data() {
return {
countDown: 10,
};
},
method: {
countDownTimer() {
const timer = setInterval(() => {
if (this.countDown > 0) {
this.countDown--;
} else {
clearInterval(timer);
}
}, 1000);
},
nextquestion(){
this.countDownTimer();
},
},
mounted() {
this.countDownTimer();
},
};
</script>
I am developing a countdown called by a function. The goal is to restart the countdown every time the user answers a question in the game and the next question is instantiated. The problem I've been facing is that every time I call the function before time runs out, the next countdown gets accelerated and skips numbers.
<template>
<div>
{{ countDown }}
</div>
</template>
<script>
export default {
data() {
return {
countDown: 10,
};
},
method: {
countDownTimer() {
const timer = setInterval(() => {
if (this.countDown > 0) {
this.countDown--;
} else {
clearInterval(timer);
}
}, 1000);
},
nextquestion(){
this.countDownTimer();
},
},
mounted() {
this.countDownTimer();
},
};
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量的研究和尝试,我能够通过一些更改解决此问题:
After a lot of research and trying, I was able to solve this with a few changes: