微信小程序实现计时器
wxml
<view class="container"> {{timecount}}</view>
<button bindtap='start'>开始</button>
<button bindtap='stop'>暂停</button>
<button bindtap='Reset'>停止</button>
js
var intt;
Page({
data: {
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
timecount: '00:00:00'
},
onLoad: function () {
},
//开始
start: function () {
var that = this;
//停止(暂停)
clearInterval(intt);
//时间重置
that.setData({
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
})
intt = setInterval(function () { that.timer() }, 50);
},
//暂停
stop: function () {
clearInterval(intt);
},
//停止
Reset: function () {
var that = this
clearInterval(intt);
that.setData({
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
timecount: '00:00:00',
})
},
timer: function () {
var that = this;
console.log(that.data.millisecond)
that.setData({
millisecond: that.data.millisecond + 5
})
if (that.data.millisecond >= 100) {
that.setData({
millisecond: 0,
second: that.data.second + 1
})
}
if (that.data.second >= 60) {
that.setData({
second: 0,
minute: that.data.minute + 1
})
}
if (that.data.minute >= 60) {
that.setData({
minute: 0,
hour: that.data.hour + 1
})
}
that.setData({
timecount: that.data.hour + ":" + that.data.minute + ":" + that.data.second + ":" + that.data.millisecond
})
},
});

浙公网安备 33010602011771号