Page({
onShow() {
let that = this;
var dates = {datetime : [{dat: 0,name:'zs'},{dat: 6,name:'ls'},{dat: 10,name:'ww'}]}
// console.log(dates) //数据
let len = dates.datetime.length; //时间数据长度
function get000Count() {
var count = 0;
for (var i = 0; i < len; i++) {
if (dates.datetime[i]['dat'] == 0) {
count = count + 1;
}
}
return count;
}
function nowTime() { //时间函数
for (var i = 0; i < len; i++) {
var intDiff = dates.datetime[i].dat; //获取数据中的时间戳
//console.log(intDiff)
var day = 0,
hour = 0,
minute = 0,
second = 0;
if (intDiff > 0) { //转换时间
day = Math.floor(intDiff / (60 * 60 * 24));
hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
if (hour <= 9) hour = '0' + hour;
if (minute <= 9) minute = '0' + minute;
if (second <= 9) second = '0' + second;
dates.datetime[i].dat--;
var str = hour + ':' + minute + ':' + second
//console.log(str)
} else {
//var str = '已结束!';
var str = '00:00:00';
if (get000Count() == len) {
console.log(get000Count())
clearInterval(timer);
}
}
dates.datetime[i].difftime = str; //在数据中添加difftime参数名,把时间放进去
}
that.setData({
wearList: dates
})
}
//nowTime();
var timer = setInterval(nowTime, 1000);
},
})
<view wx:for="{{wearList.datetime}}" wx:for-item="item" wx:key="id">
<view class='dates'>{{item.difftime}}</view>
<view class='dates'>{{item.name}}</view>
</view>