闫增涛
2024-03-11 f4425543db1c8db8c47a34c53ef477e72cfe318e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
// pages/bookServices/examination/questionSchedule/questionSchedule.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
 
  },
  created() {
    // if (props.answerType == 'option') {
    // this.startCountdown()
    this.setData({
      countdownTime: 2 * 60 * 60 * 1000
    })
    // }  
  },
  detached() {
    if (this.data.countdownInterval !== null) {
      clearInterval(this.data.countdownInterval)
    }
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    countdownInterval: null,   // 计时器
    isCountdownRunning: true, // 是否倒计时
    countdownTime: "",  // 时间
    showTime: '',
  },
  observers: {
    'countdownTime': function (newValue, oldValue) {
      const showTime = this.formatTime(this.data.countdownTime)
      this.setData({
        showTime: showTime
      })
      console.log(this.data.showTime);
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 格式化时间
    formatTime(ms) {
      const hours = Math.floor((ms / (1000 * 60 * 60)) % 24)
        .toString()
        .padStart(2, '0')
      const minutes = Math.floor((ms / (1000 * 60)) % 60)
        .toString()
        .padStart(2, '0')
      const seconds = Math.floor((ms / 1000) % 60)
        .toString()
        .padStart(2, '0')
      return `${hours}:${minutes}:${seconds}`
    },
    // 获取保存的倒计时时间
    getSavedTime() {
      const savedTime = wx.getStorageSync('countdownTime')
      return savedTime ? parseInt(savedTime) : null
    },
    // 保存倒计时时间到本地存储
    saveTime() {
      wx.setStorageSync('countdownTime', this.data.countdownTime.toString())
    },
    clearTime() {
      this.setData({
        countdownTime: 2 * 60 * 60 * 1000
      })
    },
    // 暂停或继续倒计时
    toggleCountdown() {
      if (countdownInterval) {
        clearInterval(this.data.countdownInterval)
        this.setData({
          countdownInterval: null,
          isCountdownRunning: false
        })
      } else {
        this.startCountdown()
        this.setData({
          isCountdownRunning: true
        })
      }
    },
    // 开始倒计时
    startCountdown() {
      // 如果计时器已经存在,先清除之前的计时器
      if (this.data.countdownInterval) {
        clearInterval(this.data.countdownInterval)
        this.setData({
          countdownInterval: null
        })
      }
      this.setData({
        countdownInterval: setInterval(() => {
          this.setData({
            countdownTime: this.data.countdownTime - 1000
          })
          if (this.data.countdownTime <= 0) {
            clearInterval(this.data.countdownInterval)
            this.setData({
              countdownTime: 0,
              isCountdownRunning: false
            })
          }
          this.saveTime()
        }, 1000)
      })
 
    }
  }
})