倒計時
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);
},