实现
- 首先,是两个div用来显示我们的剩余支付时间
- 然后,是倒计时函数countdown
//倒计时
countdown () {
const end = Date.parse(new Date('2020-05-05 03:59:23'))
const now = Date.parse(new Date())
const msec = end - now
console.log(msec)
if(msec<0) return;
let day = parseInt(msec / 1000 / 60 / 60 / 24)
let hr = parseInt(msec / 1000 / 60 / 60 % 24)
let min = parseInt(msec / 1000 / 60 % 60)
let sec = parseInt(msec / 1000 % 60)
this.day = day
this.hr = hr > 9 ? hr : '0' + hr
this.min = min > 9 ? min : '0' + min
this.sec = sec > 9 ? sec : '0' + sec
const that = this
if(min>=0 && sec>=0){
//倒计时结束关闭订单
if(min==0 && sec==0){
return
}
setTimeout(function () {
that.countdown()
}, 1000)
}
}
- 最后,在mounted里事先执行countdown方法
mounted () {
this.countdown()
}