写一个时分秒计时器,以90分钟为例子
data(){
return{
dataTime: '', // 倒计时时间
timer: '', // 计时器
endTime: null, // 结束时间
}
}
mounted() {
// 倒计时90分钟
this.endTime = new Date().getTime() + 90 * 60000
this.timer = setInterval(this.freshTime, 1000)
},
methods:{
/** 计时器 */
freshTime() {
var nowTime = new Date()
// 当前时间
var currentTime = parseInt((this.endTime - nowTime.getTime()) / 1000)
let h = parseInt(currentTime / (60 * 60)) % 24
let m = parseInt(currentTime / 60) % 60
let s = parseInt(currentTime % 60)
h = h > 9 ? h : '0' + h
m = m > 9 ? m : '0' + m
s = s > 9 ? s : '0' + s
this.dataTime = h + ':' + m + ':' + s
if (currentTime < 0) {
this.dataTime = '结束'
clearInterval(this.timer)
}
},
}