【VUE】vue项目开发中,setTimeout等定时器的管理。

时间:2023-03-10 03:08:09
【VUE】vue项目开发中,setTimeout等定时器的管理。

如果在一个组件中使用了定时器,当通过路由切换页面的时候

1、如果有同一个组件,定时器会叠加。

解决方案:

computed:{
timer: {
set (val) {
this.$store.state.timeout = val;
},
get() {
return this.$store.state.timeout;
}
},
},
mounted(){
if ( this.timer ) {
clearInterval(this.timer);
}
//定时发请求
var self=this;
this.timer=setInterval(function(){ //执行事件 },2000)
}

相关文章