微信小程序——秒表组件

这是一个小程序秒表组件,效果如下:
在这里插入图片描述
体验:在这里插入图片描述
组件代码:
js

Component({
  properties: {
    startTime: {
      type: Number,
      value: 0
    }
  },
  data: {
    text: '00:00.00',
    text1:'0 天 0 时'
  },
  ready() {
    this.setData({
      startTime: this.properties.startTime
    })
    this.convertTimeStampToString(Date.now() - this.properties.startTime)
  },
  methods: {
    start: function () {
      var startTime = this.properties.startTime
      var self = this
      this._interval = setInterval(function () {
        self.convertTimeStampToString(Date.now() - startTime)
      }, 33)
    },
    stop: function () {
      clearInterval(this._interval)
    },
    convertTimeStampToString:function(ts){
      var ms = String(Math.floor((1000 + Math.floor(ts) % 1000) / 10)).slice(1)
      // console.log("毫秒", ms)
      var s = String(100 + Math.floor(ts / 1000) % 60).slice(1)
      // console.log("秒", s)
      var m = Math.floor(ts / 60000) % 60
      // console.log("分钟", m)
      var h = Math.floor(ts / (60 * 60 * 1000)) % 24
      // console.log("小时", h)
      var d = Math.floor(ts / (24 * 60 * 60 * 1000))
      // console.log("天", d)
      if (m < 10) m = '0' + m
      this.setData({
        text: m + ':' + s + '.' + ms,
        text1:d+" 天 "+h+" 时"
      })
    }
  }
})

json

{
  "component": true,
  "usingComponents": {}
}

wxml

{{text1}}
{{text}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玩烂小程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值