vue 中页面销毁,定时器还在走?

1: 在mounted() {

this.timer = setInterval(() => {
    this.rechargeInfo();
}, 3000);

}创建一个定时器。
2: 切面路由跳转的时候
beforeDestroy() {

this.timer = null;
clearInterval(this.timer);

},
destroyed() {

this.timer = null;
clearInterval(this.timer);

} 进行清除
。。。
理论上组件销毁的时候定时器已经清除了。、
但是: 跳到其他页面的时候定时器依然在走。
疑问。

阅读 16.5k
3 个回答
this.timer = null;
clearInterval(this.timer);

顺序反了吧,你这样写clearInterval的参数是null 怎么能正确销毁计时器呢

clearInterval(this.timer);
this.timer = null;

改成这样试下

你要调用销毁钩子 destroy 清除定时器

this.timer = null;这个影响到了,去掉就好了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题