如果在一個組件中使用了定時器,當通過路由切換頁面的時候
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) }