vue中定时器不好清除,甚至还会叠加,主要原因是销毁页面时没有清除当前定时器,解决方法如下
1、在data中定义timer
timer:undefined,
2、创建定时器时,同时使用$once绑定销毁事件
updatePerMin(){
var _that = this
_that.timer = setInterval(function(){
_that.fetchData()
},60000)
console.log('timer:'+_that.timer)
this.$once('hook:beforeDestroy', () => {
clearInterval(_that.timer)
})
},3、在需要使用的地方使用,例如在created的时候使用
this.updatePerMin()