计算倒计时截止时间
countdown: function () {
var that = this;
var endtime = new Date('2023-04-18 23:00:00'); // 需要倒计时的结束时间
var now = new Date();
var leftTime = parseInt((endtime.getTime() - now.getTime()) / 1000);
var hours = parseInt(leftTime / 3600 % 24);
var minutes = parseInt(leftTime / 60 % 60);
var seconds = leftTime - hours * 3600 - minutes * 60;
if(leftTime>0){
that.setData({ hours: hours, minutes: minutes ,seconds:seconds })
}
},
动态展示加个定时器
Page({ data: { timer: 60 }, onLoad: function () { this.timer = setInterval(() => { if (this.data.timer > 0) { this.setData({ timer: this.data.timer - 1 }) } else { clearInterval(this.timer) } }, 1000) }})
清除定时器在生命周期函数
/**
* 生命周期函数–监听页面卸载
*/
onUnload() {
console.log(“我离开了页面onUnload()触发”)
},
触发