注意:數組
<view class='tui-countdown-content' 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>
let goodsList = [ { actEndTime: '2019/03/26 20:00:00' } ] Page({ /** * 頁面的初始數據 */ data: { countDownList: [], actEndTimeList: [] }, /** * 生命週期函數--監聽頁面加載 */ onLoad: function (options) { let endTimeList = []; // 將活動的結束時間參數提成一個單獨的數組,方便操做 goodsList.forEach(o => { endTimeList.push(o.actEndTime) }) this.setData({ actEndTimeList: endTimeList }); // 執行倒計時函數 this.countDown(); }, 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); },