关于vue渲染多个倒计时问题

直接上代码逻辑

此时拿到后端返回的秒数(返回格式为数组包含秒数)

需要转换为时分秒(00:00:00)的格式

countdown(){
         let that = this;
        //定义一个数组接收后端返回的数组 并拿到它
        this.listData.forEach((item, index) => {
        console.log(item.free_timer);
        var times = item.free_timer;
        if (times > 0) {
          for (let i = 0; i < times; i = i + 1) {
            setTimeout(function () {
              var timess = times - i;
              var h, m, s;
              if (timess > 1) {
                h = Math.floor((timess / 60 / 60) % 24);
                m = Math.floor((timess / 60) % 60);
                s = Math.floor(timess % 60);
                h = checkTime(h);
                m = checkTime(m);
                s = checkTime(s);
                function checkTime(items) {
                  if (items < 10) {
                    items = "0" + items;
                  }
                  return items;
                }
                const timer = h + ":" + m + ":" + s;
                console.log(timer);
                if (that.time.length > index) {
                  that.time = [];
                  that.time.push(timer);
                  // console.log(that.time);
                  that.listData.forEach((item, index) => {
                    that.$set(item, "time1", that.time[index]);
                  });
                } else {
                  that.time.push(timer);
                  that.listData.forEach((item, index) => {
                    that.$set(item, "time1", that.time[index]);
                  });
                }
              } else {

              //此处为倒计时结束之后想要进行的操作

              }
            }, 1000 * i);
          }
        }
      });
}

后续在需要渲染的地方渲染listData数组中的time1

需要进行倒计时的时候 调用该方法就行

以上仅为自身理解,如有错误请各位大佬指正!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值