微信小程序单个倒计时效果

  var end_time = grouponList.expire_time.replace(/-/g, /)
  grouponcountdown(that, end_time) 
//适用于商品列表倒计时/** * end_time int 结束时间 * param int 数组键 */
function grouponcountdown(that, end_time) {
  var EndTime = new Date(end_time).getTime();
  var NowTime = new Date().getTime();

  var total_micro_second = EndTime - NowTime;

  var groupons = that.data.groupon;
  groupons.expire_timestamp = dateformat(total_micro_second);
  if (total_micro_second <= 0) {
      groupons.expire_timestamp = "已结束"
  }
  that.setData({
      "groupon": groupons
  })
  setTimeout(function() {
      grouponcountdown(that, end_time);
  }, 1000)
}
// 时间格式化输出,每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  var hrStr = hr.toString();
  if (hrStr.length == 1) hrStr = 0 + hrStr;

  // 分钟
  var min = Math.floor(second / 60 % 60);
  var minStr = min.toString();
  if (minStr.length == 1) minStr = 0 + minStr;

  //
  var sec = Math.floor(second % 60);
  var secStr = sec.toString();
  if (secStr.length == 1) secStr = 0 + secStr;

  if (day < 1) {
      return "" + hrStr + ":" + minStr + ":" + secStr;
  } else {
      return "" + day + "" + hrStr + ":" + minStr + ":" + secStr;
  }
}

 

微信小程序单个倒计时效果

上一篇:微信小程序获取用户信息接口


下一篇:微信小程序-使用腾讯Wxpage