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

1.展示效果如下:

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

2.wxml代码:

<p>仅{{item.endtime}}</p>

3.js代码: 

//封装的倒计时方法 //批量倒计时 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, 'https://www.jb51.net/') grouponcountdown(that,end_time, i) } }, }) },

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6cdbd955f008fcc7328f296b1c6d25e7.html