setInterval真机测试不正常?
发布于 7 年前 作者 taojing 2593 次浏览 来自 问答

基础库2.9.4,开发工具Stable 1.02.1911180。用setInterval写了一个倒计时秒表,that.renderTime()是画表盘的函数。模拟器测试正常,真机测试时,没有按周期调用starttimePiece函数,无法正常倒计时。

starttimePiece: function () {
    const that = this
    var millisecond = that.data.millisecond
    var second = that.data.second
    var minute = that.data.minute
    that.data.timePieceTimer = setInterval(function () {
      millisecond--
      if (millisecond < 0) {
        millisecond = 99//  大于等于60秒归零    
        second--
        if (second < 0) {
          second = 59 //  大于等于60分归
          minute--
          if (minute < 10) {
            // 少于10补零
            that.setData({
              minute: '0' + minute
            })
          } else {
            that.setData({
              minute: minute
            })
          }
        }
        if (second < 10) {
          // 少于10补零
          that.setData({
            second: '0' + second
          })
        } else {
          that.setData({
            second: second
          })
        }
      }
      if (millisecond < 10) {
        // 少于10补零
        that.setData({
          millisecond: '0' + millisecond
        })
      } else {
        that.setData({
          millisecond: millisecond
        })
      }
      if (minute == 0 && second ==0) {
        that.setData({
          millisecond: '00' 
          })
        clearInterval(that.data.timePieceTimer)
      }
      that.renderTime()
    }, 10)
  },
2 回复

画图部分有点问题,但是在真机上倒计时都不行https://developers.weixin.qq.com/s/0UMFkSmt7gdi

回到顶部