根据当前时间计算倒计时,在列表中计算倒计时
最近在写一个类似拼夕夕拼团的活动,就几个人付相同的钱,即可成团。这就需要一个倒计时,来计算活动结束时间
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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) },
以上即是倒计时的写法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律