首页 > 其他分享 >根据当前时间计算倒计时,在列表中计算倒计时

根据当前时间计算倒计时,在列表中计算倒计时

时间:2023-03-20 10:02:11浏览次数:37  
标签:leftTime 60 floor 列表 倒计时 计算 24 Math

最近在写一个类似拼夕夕拼团的活动,就几个人付相同的钱,即可成团。这就需要一个倒计时,来计算活动结束时间

代码如下:

            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

相关文章

  • Qt 计算字符串和文件的md5 值
    chatgpt结果string#include<QCryptographicHash>#include<QDebug>QStringstring="Hello,World!";QByteArraydata=string.toUtf8();QByteArrayhash=QCry......
  • 资料分析而非资料计算
    通过一个礼拜的做题,最大的感觉是常识、语言和判断是可以通过刷题积累和弥补,时间均是可以把握的;数量目前处于前期,暂未遇到问题,但如果对应1分钟一道题还是要想办法去加速;但是......
  • 计算机网络原理 - 计算题汇总
    时延注意单位换算例题一例题二答:A-C发送过程无需排队,因此时间延迟就是三段链路发送延迟的和。A发送延迟=4/20=0.2s路由器1发送延迟=4/40=0.1s路由器2发送延迟=4......
  • C++面向对象、初始化列表、static const
    面向对象OOP​ OOP的四大特征:抽象、封装、继承、多态​ 对象内存大小只和成员变量有关,不同对象的变量都有自己的空间,成员方法是所有对象共享的,一旦编译会添加this指针,......
  • python语句之列表推导式
    python语句之列表推导式列表推导式是python语言特有的一种语法结构,也可以看成是python中独特的数据处理方法它在python中用转换和过滤数据语法格式:[表达......
  • flask框架,查询某个目录下文件列表,目录列表,查询是否存在某个文件
    #要查询的目录directory='/path/to/directory'#获取目录下的所有文件和目录contents=os.listdir(directory)#筛选出所有的文件files=[......
  • 【Android开发】基本组件-列表选择框
    Android中提供的列表选择框(Spinner)相当于在网页中常见的下拉列表框,通常用于提供一系列可选择的列表项共用户选择。Android中,可以使用两种方法向屏......
  • 希望计算机专业同学都知道这些宝藏博主
    本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校招......
  • 希望计算机专业同学都知道这些宝藏博主
    本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校......
  • 计算机网络----应用层
    《域名系统》《什么是域名系统?》域名系统DNS(domainnamesystem)是互联网使用的命名系统《为何需要域名系统?》应用层中问题的解决是通过......