data () {
return {
usermsg:{} ,
stime:null,
h:'00',
m:'00',
s:'00'
}
},
// 倒计时开始考试
gettimedown(){
let statrdata = this.usermsg.stime
statrdata = '2031-07-01 18:19:00' //开始时间
if(statrdata){
if(Date.parse(new Date())<new Date(statrdata)){ //Date.parse(new Date())当前时间
this.stime =new Date(statrdata) - Date.parse(new Date())
this.countTime();
}else{
this.$message({
message: '答题时间开始!',
type: 'success'
});
this.start();
}
}
},
countTime () {
const time = this.stime / 1000
if (!this.timer) {
this.count = time
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= time) {
this.count--
const h = parseInt((this.count) / (60 * 60))
const m = parseInt((this.count) / 60 % 60)
const s = parseInt((this.count) % 60)
if (this.h === 0 && this.m === 0 && this.s === 0) {
this.$message({
message: '答题时间开始!',
type: 'success'
})
this.start()
} else {
this.h = h > 9 ? h : '0' + h
this.m = m > 9 ? m : '0' + m
this.s = s > 9 ? s : '0' + s
}
} else {
this.$message({
message: '答题时间开始!',
type: 'success'
});
clearInterval(this.timer);
this.timer = null;
this.start()
}
}, 1000)
}
},