123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- Page({
- data: {
- id: 1 , // 初始ID值
- // isLoading: true,//授权加载
- countdown: 120,
- progressWidth: 100, // 初始进度条宽度为100%
- totalSeconds: 120, // 总秒数
- currentSeconds: 120 // 当前剩余秒数
- },
- onLoad() {
- //id=1计时器
- // this.setData({
- // isLoading: false
- // });
- // setTimeout(() => {
- // this.setData({
- // isLoading: false
- // });
- // }, );
- //id=2计算器
- this.countDown();
- },
- countDown() {
- let timer = setInterval(() => {
- let { currentSeconds, totalSeconds } = this.data;
- currentSeconds--;
- if (currentSeconds < 0) {
- currentSeconds = totalSeconds;
- }
- let aprogressWidth = (currentSeconds / totalSeconds) * 100;
- this.setData({
- currentSeconds: currentSeconds,
- progressWidth: aprogressWidth
- });
- }, 1000);
- this.data.timer = timer; // 保存定时器,以便在onUnload中清除
- },
- onUnload() {
- if (this.data.timer) {
- clearInterval(this.data.timer);
- }
- }
- });
|