Page({
data: {
timer: null,
countDownNum: 10,
seconds: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad() {
this.startCountDown();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
clearInterval(this.timer2);
this.data.seconds = 0;
},
//开始计时
startCountDown() {
if (this.timer) {
clearInterval(this.timer)
}
this.timer = setInterval(this.startTimer, 1000)
},
//+1秒
startTimer() {
if (this.data.seconds > this.data.countDownNum) {
clearInterval(this.timer);
this.data.seconds = 0;
//倒计时结束操作
return;
}
this.setData({
seconds: this.data.seconds+1
});
}
})
小程序倒计时,秒表,读秒,计时器
于 2024-04-30 16:18:42 首次发布