多个倒计时同时显示

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);
        }

 

posted @ 2022-03-24 17:00  龙卷风吹毁停车场  阅读(83)  评论(0)    收藏  举报