问题代码:当数值达到29时,对定时器及逆行删除,删除不了,仍在执行
_this.count = 0
this.bomTimer = setInterval(() => {
console.log('******')
_this.count++
if (_this.count >= 29) {
clearInterval(_this.bomTimer)
}
}, 36)
原因:定时器可能在其他代码处被使用,代码中找不到使用痕迹,单当输出定时器编号时,进行了跳数输出,中间的定时器编号没有被清除。
解决办法:预防定时器没被清除再次使用,在使用定时器之前,对定时器进行一次清除操作。
_this.count = 0
clearInterval(_this.bomTimer) // 进行一次清楚操作
this.bomTimer = setInterval(() => {
console.log('******')
_this.count++
if (_this.count >= 29) {
clearInterval(_this.bomTimer)
}
}, 36)
亲测可用。