gpt4 book ai didi

javascript - vuejs定时器组件重启/停止

转载 作者:行者123 更新时间:2023-12-03 00:36:48 25 4
gpt4 key购买 nike

我必须设置一个 10 分钟的计时器,它会重定向到主屏幕。此外,它必须在每个操作(例如按下按钮)时重置。我找到了这个计时器:https://github.com/fengyuanchen/vue-countdown是否可以通过某个操作重新启动它?

<countdown ref="countdown" @end="dosmth()" :time="time" :auto-start="false">
<template slot-scope="props">{{ props.seconds }}</template>
</countdown>

mounted() {
this.$refs.countdown.start();
},


dosmth: function(){
this.$refs.countdown.start();
}

这应该重新启动计时器,但即使这样也不起作用:

篮子.vue:378[Vue warn]:“end”事件处理程序中出现错误:“InternalError:递归过多”

也许有人可以帮助我?

最佳答案

您可以使用 setInterval 来执行此操作,并在每次操作点击时将值重置为初始值:

const TEN_MINUTES = 60 * 10

new Vue({
el: '#app',
data () {
return {
timer: TEN_MINUTES
}
},
filters: {
minutesAndSeconds (value) {
var minutes = Math.floor(parseInt(value, 10) / 60)
var seconds = parseInt(value, 10) - minutes * 60
return `${minutes}:${seconds}`
}
},
mounted () {
setInterval(() => {
this.timer -= 1
}, 1000)
},
methods: {
someAction () {
this.timer = TEN_MINUTES
},
someOtherAction () {
this.timer = TEN_MINUTES
}
},
template: `<div><div>Time Remaining: {{ timer | minutesAndSeconds }}</div><br><br><button @click="someAction">Some Action</button><button @click="someOtherAction">Some Other Action</button></div>`
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>

关于javascript - vuejs定时器组件重启/停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com