计时器
setInterval: function (valTime) {
var nowDate = new Date()
nowDate.setTime(nowDate.getTime() + 2000)
var minusResult = nowDate.getTime() - new Date(valTime).getTime()
const that = this
//计算出相差天数
var days = Math.floor(minusResult / (24 * 3600 * 1000))
//计算出小时数
var leave1 = minusResult % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000))
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
var minute = Math.floor(leave2 / (60 * 1000))
//计算相差秒数
var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
var second = Math.round(leave3 / 1000)
that.setData({
minute: minute < 10 ? '0' + minute : minute,
second: second < 10 ? '0' + second : second
})
if (hours >= 1) {
that.setData({
minute: 59,
second: 59
})
return
}
setInterval(function () { // 设置定时器
second++
if (second >= 60) {
if (minute == 59) {
return
}
second = 0 // 大于等于60秒归零
minute++
if (minute >= 60) {
minute = 0 // 大于等于60分归零
hours++
if (hours < 10) {
// 少于10补零
that.setData({
hours: '0' + hours
})
} else {
that.setData({
hours: hours
})
}
}
that.setData({
minute: minute < 10 ? '0' + minute : minute
})
}
that.setData({
second: second < 10 ? '0' + second : second
})
}, 1000)
},
})