在项目中,咱们常常会使用到定时器setInterval(),但是不少时候咱们会发现,即便我退出当前页面,定时器依然在工做,很是消耗内存,因此咱们要进行手动清理:this
将定时器保存在变量中,退出页面时清除变量内存
1.定义空的变量io
data: function (){function
return {变量
timer: null定时器
}im
}项目
2.定义定时器di
methods: {工作
setTimer: function () {
this.timer = setInterval( () => {
.....
}, 1000)
}
}
3.进入和退出时清除定时器
mounted() {
clearInterval(this.timer)
},
distroyed: function () {
clearInterval(this.timer)
}