vue中的倒計時實現(xiàn)方法
js部分
// 獲取現(xiàn)在時間
gettime(){
let nowtime = new Date()
let nowmiao = nowtime.getTime()//現(xiàn)在的秒數(shù)
let settime = new Date("2060-11-30 13:22:20")
let setmiao = settime.getTime()//要設置的秒數(shù)
let toolmiao =setmiao-nowmiao//秒數(shù)差
if (toolmiao>=0) { //賦值
this.myday = Math.floor(toolmiao/1000/60/60/24);
this.myh = Math.floor(toolmiao/1000/60/60%24);
//補0操作
if(this.myh<10){
this.myh = "0"+this.myh
}
this.mym = Math.floor(toolmiao/1000/60%60);
//補0操作
if(this.mym<10){
this.mym = "0"+this.mym
}
//補0操作
this.mys = Math.floor(toolmiao/1000%60);
if(this.mys<10){
this.mys = "0"+this.mys
}
}
},
1.settime需要一個標準格式的時間
2.在mounted中
mounted() { let timer = setInterval(() => { this.gettime() }, 1000); },
浙公網(wǎng)安備 33010602011771號