进入页面时,将定时器赋给data中一个变量
_this.setData({
start_state: 1,
start_time: time, //开始倒计时S
start_timer: setInterval(() => {
let t = _this.data.start_time
t -= 1;
_this.setData({
start_time: t
})
if (_this.data.start_time == 0) {
clearInterval(_this.data.start_timer);
_this.setData({
start_state: 2,
})
}
}, 1000)
})
离开页面,清除定时器
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
console.log('页面走了onHide');
clearInterval(this.data.start_timer);
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
clearInterval(this.data.start_timer);
},