微信小程序开发秒表计时器

https://www.jianshu.com/p/c61028d7d509

https://blog.csdn.net/Missbelover/article/details/93749680?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

wxml:
{{hours}}:{{minute}}:{{second}}



wxss:
button{
  width:150rpx;
  background: rgb(7, 193, 96);
  color: #fff;
  margin-bottom: 8px;
}

wx.js:

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    timer:null,
    hours: '0' + 0,   // 时
    minute: '0' + 0,   // 分
    second: '0' + 0    // 秒
  },
  onLoad: function () {
  },
 
//计时开始
  start: function () {
    let _this = this;
    _this.setInterval();
  },
 
  // 计时器
  setInterval: function () {
    const _this = this
    var second = _this.data.second
    var minute = _this.data.minute
    var hours = _this.data.hours
    _this.data.timer = setInterval(function () {  // 设置定时器
      second++
      if (second >= 60) {
        second = 0  //  大于等于60秒归零
        minute++
        if (minute >= 60) {
          minute = 0  //  大于等于60分归零
          hours++
          if (hours < 10) {
            // 少于10补零
            _this.setData({
              hours: '0' + hours
            })
          } else {
            _this.setData({
              hours: hours
            })
          }
        }
        if (minute < 10) {
          // 少于10补零
          _this.setData({
            minute: '0' + minute
          })
        } else {
          _this.setData({
            minute: minute
          })
        }
      }
      if (second < 10) {
        // 少于10补零
        _this.setData({
          second: '0' + second
        })
      } else {
        _this.setData({
          second: second
        })
      }
    }, 1000)
  },

//暂停
  stop: function () {
    let _this = this;
    clearInterval(_this.data.timer);
  },
})
millisecond: 0,

setInterval(function () {
      that.timer()
}, 50);//毫秒

let millisecond = this.data.millisecond;
if (millisecond >= 100) {
    millisecond  = 0  //  大于等于100
    second ++
}

 

你可能感兴趣的:(微信小程序)