如图所示:按逻辑已经可以清除,但是当页面跳转后,定时器任然在执行
beforeDestroy(){
clearInterval(this.avltimer)
this.avltimer = null
},
mounted(){
this.avltimer = setInterval(() => {
/*去做的事*/
this.getdata() //请求数据方法
}, 2000);
},
methods:{
async getdata(){
let res = await .... //请求数据
if(res){
//事情
}
}
}
解决方法: 不使用async await 直接promise回调
beforeDestroy(){
clearInterval(this.avltimer)
this.avltimer = null
},
mounted(){
this.avltimer = setInterval(() => {
/*去做的事*/
this.getdata() //请求数据方法
}, 2000);
},
methods:{
//将异步去掉
getdata(){
(请求方法Promise).then((res) => {
//事情
}).catch(( => {}))
}
}