循环渲染倒计时

应用场景 拼团倒计时和 单个倒计时都能用到

timeSpanStr(unixtime) {
    let dateTime = new Date(parseInt(unixtime) * 1000)
    let year = dateTime.getFullYear();
    let month = dateTime.getMonth() + 1;
    let day = dateTime.getDate();
    let hour = dateTime.getHours();
    let minute = dateTime.getMinutes();
    let second = dateTime.getSeconds();
    let now = new Date();
    let now_new = Date.parse(now.toDateString()); //typescript转换写法
    let milliseconds = now_new - dateTime;
    let timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    return timeSpanStr;
  },
  countDown: function() {
    var that = this;
       // 这里循环的数据是你订单
    for (var x = 0; x <= this.data.endTime.length - 1; x++) {
	//	如果是时间戳放到timeSpanStr()这个函数里面转换为2019-12-20格式
      let p = this.data.endTime[x].end_time
      var a = this.timeSpanStr(p)
      // 兼容ios
      //这里也可以不用写可以把上面的'-' 改了 我没试过
      let remainDate = a
      let newRemainDate = remainDate.replace(/-/g, "/")

      let nowTime = new Date().getTime(); //现在时间(时间戳)
      let endTime = new Date(newRemainDate).getTime(); //结束时间(时间戳
      var time = (endTime - nowTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      day = that.timeFormin(day),
        hou = that.timeFormin(hou),
        min = that.timeFormin(min),
        sec = that.timeFormin(sec)
      this.data.detaildata[x].day = day
      this.data.detaildata[x].hou = hou
      this.data.detaildata[x].min = min
      this.data.detaildata[x].sec = sec
      this.setData({
        detaildata: this.data.detaildata
      })
    }
    // 每1000ms刷新一次
    if (time > 0) {
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    } else {
      that.setData({
        countDown: false
      })
    }
  },
  //小于10的格式化函数(2变成02)
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数(不会出现负数)
  timeFormin(param) {
    return param < 0 ? 0 : param;
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值