vue中beforeDestroy销毁定时器不生效
data() {
return {
nowPageTime: 0, //当前页面时间,用于计时
};
},
created() {
this.timeRefresh();
},
methods: {
timeRefresh(){
let timeRefName = setInterval((res) => {
this.nowPageTime += 1
if(this.nowPageTime == 1800){ //若计时半小时了
this.$router.go(0);
}
},1000)
// 通过 $once 来监听定时器,在 beforeDestroy 钩子可以被清除。
this.$once('hook:beforeDestroy', () => {
clearInterval(timeRefName);
timeRefName = null;
})
},
}