2022-11-18 clearInterval(定时器)无法完全关闭定时器
问题描述:vue+uniapp之小程序定时器业务
原代码:clearInterval(this.timer)
this.timer为定时器的容器,多此点击开启定时器然后就会发现你想使用clearInterval(this.timer)来关闭定时器会发现它还是在运行
代码:
// 开启 startTimer() { this.timer = setInterval(() => { this.viime++; }, 1000); }, // 关闭 closeTimer() { if (this.timer) { clearInterval(this.timer); this.timer = null; } },