vue中两套定时器,一个是windows对象的,另一个是vue/node.js 封装的,需要引用
import { setInterval, clearInterval } from 'timers'
一般我们写
this.timer = setInterval(() => {
this.getRealData()
}, 10000)
默认使用vue封装的,它会自动引用
import { setTimeout } from 'timers'
但是它只引用了一半,只引用了setTimeout 没有clearInterval 会出现清不掉的情况。所以还是自己手动引用比较好
建议使用windows对象的定时器,使用方法
window.clearInterval(this.timer)
定时器在created定义,在beforeDestory中清楚
created() {
this.$nextTick(() => {
this.getRealData()
this.getDriver()
})
this.timer = setInterval(() => {
this.getRealData()
}, 10000)
}
beforeDestroy() {
if (this.timer) {
clearInterval(this.timer) // 关闭
}
}