问题:从第一个页面跳转到第二个页面后,如果停留在第二个页面,定时器还在运行。如果在两个页面之间来回跳转,跳转时间小于定时器的间隔时间时,也会出现重复创建setTimeout的情况。
原因:当我们刷新页面时,会将当前页面之前创建的setTimeout以及其他定时器都清除掉,但是仅仅是路由切换是不会清除的。
data () {
return {
ct: null
}
},
methods: {
start() {
this.ct= setTimeout( () => {that.countdown(end)}, 1000)
}
end() {
clearTimeout(this.ct); //清除
}
}
setInterval()计时也需要clearInterval()来清除