wxml
<view class='tui-countdown-content font10pt' wx:for="{{countDownList}}" wx:key="countDownList"> <text class='tui-conutdown-box'>{{item.day}}</text>天 <text class='tui-conutdown-box'>{{item.hou}}</text>时 <text class='tui-conutdown-box'>{{item.min}}</text>分 <text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>秒 </view>css
/*倒计时样式*/ .tui-countdown-content{ height: 100rpx; line-height: 100rpx; text-align: center; background-color: #fff; margin-top: 15rpx; padding: 0 15rpx; font-size: 28rpx; } .tui-conutdown-box{ display: inline-block; height: 46rpx; width: 46rpx; line-height: 46rpx; text-align: center; background-color: #000; color: #fff; margin: 0 15rpx; } .tui-countdown-bg{ background-color: red; }js
data: { actEndTimeList: [], }, onLoad: function (options) { }, timeFormat(param) {//小于10的格式化函数 return param < 10 ? '0' + param : param; }, countDown() {//倒计时函数 // 获取当前时间,同时得到倒计时结束时间数组 let newTime = new Date().getTime(); let endTimeList = this.data.actEndTimeList; let countDownArr = []; // 对结束时间进行处理渲染到页面 endTimeList.forEach(o => { let endTime = new Date(o).getTime(); let obj = null; // 如果倒计时未结束,对时间进行处理 if (endTime - newTime > 0) { let time = (endTime - newTime) / 1000; // 获取天、时、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hou = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: this.timeFormat(hou), min: this.timeFormat(min), sec: this.timeFormat(sec) } } else {//倒计时已结束,全部设置为'00' obj = { day: '00', hou: '00', min: '00', sec: '00' } } countDownArr.push(obj); }) // 渲染,然后每隔一秒执行一次倒计时函数 this.setData({ countDownList: countDownArr }) setTimeout(this.countDown, 1000); },