微信小程序商城拼团商品、抢购商品倒计时

时间:2021-07-15 19:48:11
//单个倒计时,适用用于单个商品的倒计时
function countdown(that) {
  var EndTime = that.data.end_time || [];//倒计时的结束时间
  var NowTime = new Date().getTime();//当前时间

  var total_micro_second = EndTime - NowTime || [];//剩余的微秒

  // 渲染倒计时时钟
  that.setData({
    count: dateformat(total_micro_second)
  });
  if (total_micro_second <= 0) {
    that.setData({
      count: "已经截止"
    });
    return;
  }
  setTimeout(function () {
    countdown(that);
  }, 1000)
}
//适用于商品列表倒计时
/** * end_time int 结束时间 * param int 数组键 */
function grouponcountdown(that, end_time,param){
  var EndTime = end_time  || [];
  var NowTime = new Date().getTime();

  var total_micro_second = EndTime - NowTime || [];

  var groupons = that.data.groupon;
  groupons[param].endtime = dateformat(total_micro_second);
  if (total_micro_second <= 0) {
      groupons[param].endtime = "已经截止"
   }
  that.setData({
    groupon: groupons
  })

    setTimeout(function () {
    groupcount(that, end_time,param);
  }, 1000)
}
// 时间格式化输出,如11:03 25:19 每1s都会调用一次
function dateformat(micro_second,t = 0) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  if (t == 0) {
    return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  }else{
    return  hr + "小时" + min + "分钟" + sec + "秒";
  }
}


//商品列表获取到数据进行遍历
for (var i = 0; i < (that.data.group.length); i++) {
  grouponcountdown(that, (that.data.group[i].endtime),i)
}

微信小程序 交流群 欢迎加群讨论:578385176