需求: 寫一個活動倒計時功能
獲取當前時間戳
const current_time = Math.round(new Date().getTime() / 1000).toString()
大概程序
后臺會回傳一個活動開始時間的時間戳,你需要把活動時間戳減去當前時間戳, 再將剩下的轉換為時分秒
初始化的時候 定義一個定時器
clearInterval(this.newTimer)
this.newTimer = setInterval(this.CountDown, 1000)
methods:{
// 給數子補 0
timeAdd(str) {
if (str.length <= 1) {
str = '0' + str
}
return str
},
CountDown(){
if (this.current_time >= 0) {
let hour = Math.floor(this.current_time / 3600)
let minutes = Math.floor((this.current_time / 60) % 60)
let seconds = Math.floor(this.current_time % 60)
if (hour === 0 && minutes === 0 && seconds === 0) {
// this.$alert('活動開始')
}
--this.maxtime
this.msg =
'活動剩余' +
this.timeAdd(String(hour)) +
'時' +
this.timeAdd(String(minutes)) +
'分' +
this.timeAdd(String(seconds)) +
'秒'
} else {
clearInterval(this.newTimer)
this.msg = '活動結束'
}
},
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22113.html
標籤:python
