最近在写一个类似拼夕夕拼团的活动,就几个人付相同的钱,即可成团。这就需要一个倒计时,来计算活动结束时间
代码如下:
var that = this; var h,m,s,t; var leftTime =that.classInfo.countdown; //这个时间是倒计时的毫秒数,我这里是由后台返的,也可自行计算(既活动结束时间转化成毫秒减去当前时间转化成毫秒),若为负数或者为0,则当前倒计时结束 console.log(leftTime); that.cxtimer = setInterval(function () { leftTime--; if (leftTime>0){ h = Math.floor(leftTime/60/60/24); m = Math.floor(leftTime/60/60) - h*24; s = Math.floor(leftTime/60 - h*24*60 - m*60); t = Math.floor(leftTime - s*60 - m*60*60 - h*24*60*60) // console.log(h,m,s) that.cxh = h<10?'0'+h:h;//这里是天,时,分,秒,等 that.cxm = m<10?'0'+m:m; that.cxs = s<10?'0'+s:s; that.cxt = t<10?'0'+t:t; // console.log(that.cxh); } if(Number(leftTime)<=1){ that.cxh = '00' that.cxm = '00' that.cxs = '00' that.cxt = '00' clearInterval(that.cxtimer); } }.bind(that),1000)
如果在一个列表中展示倒计时,该如何实现呢,代码如下:
timer() { //页面多个定时器 //主要逻辑都在这页面更新 let that = this; that.list = that.classInfo.order;//这是需要展示倒计时的列表 var listtime= that.listtime; for(var i=0;i<that.list.length;i++){ that.listtime[i] = '' var item = that.list[i]; that.timeradd(item,i,1); } }, timeradd(item,i){ var that = this; var h,m,s,t,rens; var leftTime = item.countdown;//列表中每一项的倒计时 that.timergroup = setInterval(function () { leftTime--; if (leftTime>0){ h = Math.floor(leftTime/60/60/24); m = Math.floor(leftTime/60/60) - h*24; s = Math.floor(leftTime/60 - h*24*60 - m*60); t = Math.floor(leftTime - s*60 - m*60*60 - h*24*60*60); rens = h+'天'+m+'时'+s+'分'+t+'秒'; }else{ rens = '0天0时0分0秒'; clearInterval(that.timergroup); } that.listtime[i] = rens; that.$forceUpdate()//强制刷新数据 }.bind(that),1000) },
以上即是倒计时的写法
标签:leftTime,60,floor,列表,倒计时,计算,24,Math From: https://www.cnblogs.com/bingchenzhilu/p/17235302.html