【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) 
  }

 

posted @ 2018-05-14 17:33  freeah  阅读(2530)  评论(0编辑  收藏  举报