多個倒計時同時顯示
阿新 • • 發佈:2022-03-24
this.objdatas.forEach(item => { this.temp = setInterval(() => { let time = new Date(item.createdDate).getTime() item['countDown'] = this.countDownFun(time) this.$forceUpdate() },1000) })
countDownFun(time) { time--; let nowTime = new Date().getTime(); // 獲取當前時間 if (time >= nowTime) { var secondTime = 0 //秒 var minuteTime = 0; // 分 var hourTime = 0; // 小時 vartoday = 0 //天 // 全部剩餘多少秒 var seconds = Math.ceil((time - nowTime) / 1000) hourTime = Math.floor(seconds / 3600) //天數 today = Math.floor(hourTime / 24) //小時 hourTime = Math.floor(hourTime % 24) < 10 ? '0' + Math.floor(hourTime % 24) : Math.floor(hourTime % 24) // 分 minuteTime = Math.floor(seconds / 60 % 60) < 10 ? '0' + Math.floor(seconds / 60 % 60) : Math.floor(seconds / 60 % 60) //秒 secondTime = Math.floor(seconds % 60) < 10 ? '0' + Math.floor(seconds % 60) : Math.floor(seconds % 60) console.log('剩餘'+today+'天'+ hourTime+'小時'+minuteTime+'分鐘'+secondTime+'秒') return '剩餘'+today+'天'+ hourTime+'小時'+minuteTime+'分鐘'+secondTime+'秒' } else { return "00:00:00:00"; } }
destroyed() { //切記頁面銷燬需要銷燬 clearInterval(this.temp); }