【发布时间】:2021-08-20 20:27:16
【问题描述】:
我有一个在 mounted() 钩子中调用的方法,它使用 setInterval 设置时间间隔,如下所示:
methods: {
clock() {
const date = new Date()
const hours = ((date.getHours() + 11) % 12 + 1);
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const hour = hours * 30;
const minute = minutes * 6;
const second = seconds * 6;
}},
mounted: function() {
setInterval(this.clock, 1000);
},
还有另一个函数可以清除beforeDestroyed 挂钩中的间隔。 console.log 输出“workBefore”,所以我知道,它进入了函数内部,但没有清除间隔。下面是函数:
beforeDestroyed: function() {
console.log("workBefore")
clearInterval(this.clock);
}
感谢任何帮助!!!
【问题讨论】:
标签: javascript vue.js lifecycle