这是我做的倒计时功能,本意是打算用destroyed函数在离开页面时停止倒计时,可是我用了很多方法都不行,请大大们帮忙看下//设置倒计时
setIntervalForTime2(Pinyin, Id) {
clearInterval(this.interval2[Pinyin]); this.interval2[Pinyin] = window.setInterval(() => { //倒计时数字
var h = Math.floor(this.openTime[Pinyin] / 3600) var m = Math.floor((this.openTime[Pinyin] / 60 % 60)) var s = Math.floor((this.openTime[Pinyin] % 60)) let hour = h < 10 ? "0" + h : h let min = m < 10 ? "0" + m : m let sec = s < 10 ? "0" + s : s let time = ''
time = hour + ":" + min + ":" + sec this.endOpenTime1[Pinyin] = time if (this.openTime[Pinyin] <= 0) { this.getOpenTime2(Pinyin, Id)
} this.openTime[Pinyin]--
}, 1000);
},destroyed函数是我在网上找的写法,好像也行不通,离开这个页面后还是会不停的请求这个函数this.getOpenTime2(Pinyin, Id) destroyed() { for (let index in this.interval2) { window.clearInterval(index)
}
},
1 回答
万千封印
TA贡献1891条经验 获得超3个赞
destroyed() { for (let index in this.interval2) {- window.clearInterval(index)+ window.clearInterval(this.interval2[index]) } },
添加回答
举报
0/150
提交
取消