支付宝小程序计时器
1.引入插件timer.js
var Timer= require(’…/…/utils/timer.js’);
2.在 onLoad 周期初始化,第二個(gè)參數(shù)17為需要計(jì)時(shí)的總秒數(shù)
onLoad: function () {
this.timer= new Timer(this,17);
}
3. 在任意的方法中,調(diào)用start方法觸發(fā)倒計(jì)時(shí)
this.timer.start();
// 倒計(jì)時(shí)
class Timer{
constructor(page, second) {
this.page = page;
this.page.setData({ time: second});
this.initSeconds = second;
}
start() {
console.log(‘進(jìn)入倒計(jì)時(shí)’);
var that = this;
var seconds = that.initSeconds;
that.page.setData({ time: seconds});
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(
() => {
seconds -= 1;
that.page.setData({
time: seconds,
})
if (seconds <= 0) {
that.page.setData({
time: ‘0’
})
resolve(setTimer)
}
}
, 1000)
})
promise.then((setTimer) => {
console.log(‘清除計(jì)時(shí)器’);
clearInterval(setTimer)
})
}
}
module.exports = Timer;
————————————————
版權(quán)聲明:本文為CSDN博主「勾 踐」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44510465/article/details/102687355
總結(jié)
- 上一篇: 基于小程序的家校互动平台
- 下一篇: ZCMU—1946