WeChat applet realizes bulk countdown function

The example of this article shared the specific code of the WeChat applet to achieve bulk countdown. For your reference, the specific content is as follows

// Applicable to the product list countdown / ** * end_time int end time * param INT Array key * /

微信小程序实现批量倒计时功能

2.WXML code:

  

only {{item.endtime}}

3.JS code:

// Package countdown method // Batch countdown Function GrouponCountDown (That, end_time, param) {var endtime = new date (end_time) .gettime (); // console.log (endtime); var nowtime = new date (). getTime (); var total_micro_second = EndTime – nowTime; var groupons = that.data.groupon; // console.log (groupons); groupons [param] .endtime = dateformats (total_micro_second); if (total_micro_second
  <= 0) {
  groupons[param].endtime = "已结束"
 }
 that.setData({
  groupon: groupons
 })
 setTimeout(function () {
  grouponcountdown(that, end_time, param);
 }, 1000)
}
 
// 时间格式化输出,每1s都会调用一次
function dateformats(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;
 }
}
//end
 
var app=getApp()
Page({
 
 /**
  * 页面的初始数据
  */
 data: {
   collageTeamlist : {}
 },
 
 /**
  * 生命周期函数--监听页面加载
  */
 onLoad: function (options) {
   app.showLoading();
   var that = this
   
   wx.request({
     success:function(res){
       var grouponList = request.data.collageTeamlist
       // console.log(grouponList);
       for (var i = 0; i < grouponList.length; i++) {
         var lack_num = grouponList[i].create_num - grouponList[i].current_num
         grouponList[i].lack_num = lack_num
       }
 
       that.setData({
         groupon: grouponList
       })
       var data = that.data.groupon
       //列表获取到数据进行遍历
       for (var i = 0; i < data.length; i++) {
         var end_time = data[i].end_time.replace(/-/g, '/')
         grouponcountdown(that,end_time, i)
       }
     },
   })
 }, The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Cloud. 
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment