39 lines
880 B
JavaScript
39 lines
880 B
JavaScript
|
Page({
|
|||
|
|
|||
|
data: {
|
|||
|
// 初始倒计时字符串
|
|||
|
countdown: '30分00秒'
|
|||
|
},
|
|||
|
|
|||
|
onLoad(options) {
|
|||
|
// 启动倒计时(总秒数 = 29*60 + 17)
|
|||
|
this.initCountdown(30 * 60 );
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 初始化倒计时
|
|||
|
* @param {number} totalSeconds 初始总秒数
|
|||
|
*/
|
|||
|
initCountdown(totalSeconds) {
|
|||
|
this.countdownTimer = setInterval(() => {
|
|||
|
if (totalSeconds <= 0) {
|
|||
|
clearInterval(this.countdownTimer);
|
|||
|
this.setData({ countdown: '00分00秒' });
|
|||
|
return;
|
|||
|
}
|
|||
|
totalSeconds--;
|
|||
|
const m = Math.floor(totalSeconds / 60);
|
|||
|
const s = totalSeconds % 60;
|
|||
|
const mm = m < 10 ? '0' + m : '' + m;
|
|||
|
const ss = s < 10 ? '0' + s : '' + s;
|
|||
|
this.setData({ countdown: `${mm}分${ss}秒` });
|
|||
|
}, 1000);
|
|||
|
},
|
|||
|
|
|||
|
onUnload() {
|
|||
|
// 页面卸载时清除定时器
|
|||
|
clearInterval(this.countdownTimer);
|
|||
|
}
|
|||
|
|
|||
|
});
|