1. 程式人生 > 其它 >多個倒計時同時顯示

多個倒計時同時顯示

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; // 小時
                    var
today = 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);
        }