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

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

解决方案:

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

             //执行事件

     },2000) 
  }

猜你喜欢

转载自www.cnblogs.com/zhaixr/p/9037205.html
今日推荐