1. 程式人生 > >微信小程式商城拼團商品、搶購商品倒計時

微信小程式商城拼團商品、搶購商品倒計時

//單個倒計時,適用用於單個商品的倒計時
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