小程序裏面的倒計時

Page({
  onLoad() {
    this.time()
  },
  data: {
    nowTime: 1539089166,
    overTime: 1539175560,
    time: null,
    timer: null,
    content: '',
    flag: true
  },
  time() {
    this.setData({
      timer: setInterval(() => {
        this.countDown()
        let time = this.data.time
        time = time - 1
        this.setData({
          time: time
        })
      }, 1000)
    })
  },
  countDown() {
    let {
      overTime,
      nowTime,
      timer
    } = this.data
    let time
    if (overTime < nowTime) {
      clearInterval(timer)
      this.setData({
        flag: false
      })
      return true
    } else {
      if (!this.data.time) {
        let temporary = overTime - nowTime
        this.setData({
          time: temporary
        })
      }
      time = this.data.time
      if (time === 0) {
        clearInterval(timer)
        this.setData({
          flag: false
        })
        return true
      }
      let day, hour, minute, second;
      day = Math.floor(time / (60 * 60 * 24))
      hour = Math.floor((time % (60 * 60 * 24)) / (60 * 60))
      minute = Math.floor(((time % (60 * 60 * 24)) % (60 * 60)) / 60)
      second = Math.floor(((time % (60 * 60 * 24)) % (60 * 60)) % 60)
      this.setData({
        content: `${day}天${hour}時${minute}分${second}秒`
      })
    }
  },
  onUnload() {
    clearInterval(this.data.timer)
  }

})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章