var counteventtimes = [];
Page({
data: {
memberimgUrls: [{
"id": 1,
"name": "1111",
"list": [{
"start_time": "1590064200",
"end_time": "1590150600",
"status": "going"
}]
},
{
"id": 5,
"name": "3332",
"desc": "444",
"list": [{
"start_time": "1590064200",
"end_time": "1590150600",
"status": "going"
}, {
"start_time": "1590060600",
"end_time": "1590147000",
"status": "going"
}, {
"start_time": "1590033600",
"end_time": "1590638400",
"status": "going"
}, {
"start_time": "1590134400",
"end_time": "1590220800",
"status": "going"
}]
}
]
},
goToIndex(){
wx.switchTab({
url: '../index/index',
})
},
onShow: function () {
for (var i = 0; i < this.data.memberimgUrls.length; i++) {
clearTimeout(counteventtimes[i]);
if (this.data.memberimgUrls[i].list.length > 0) {
for (var j = 0; j < this.data.memberimgUrls[i].list.length; j++) {
this.data.memberimgUrls[i].list[j].counttime = '00:00:00';
if (this.data.memberimgUrls[i].list[j].status == 'going') {
this.countEventTime(this.data.memberimgUrls[i].list[j].end_time, i, j, this.data.memberimgUrls[i].list[j].status)
}
}
}
}
},
countEventTime(endtime, frindex, secindex, status) {
console.log(1)
var stime = Date.parse(new Date()) / 1000,
endtime = endtime,
status = status,
frindex = frindex,
secindex = secindex;
var leftTime = endtime - stime;
var d, h, m, s;
d = Math.floor(leftTime / 60 / 60 / 24);
if (leftTime > 0) {
h = d * 24 + Math.floor(leftTime / 60 / 60 % 24);
m = Math.floor(leftTime / 60 % 60);
s = Math.floor(leftTime % 60);
this.data.memberimgUrls[frindex].list[secindex].percent = (((Date.parse(new Date()) / 1000 - this.data.memberimgUrls[frindex].list[secindex].start_time) / (this.data.memberimgUrls[frindex].list[secindex].end_time - this.data.memberimgUrls[frindex].list[secindex].start_time)).toFixed(2)) * 100;
this.setData({
memberimgUrls: this.data.memberimgUrls
})
} else if (leftTime <= 0) {
if (status == 'going') {
h = 0;
m = 0;
s = 0;
} else {
h = 0;
m = 0;
s = 0;
}
}
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
if (d <= 0) {
this.data.memberimgUrls[frindex].list[secindex].counttime = h + ':' + m + ':' + s;
this.setData({
memberimgUrls: this.data.memberimgUrls
})
} else {
this.data.memberimgUrls[frindex].list[secindex].counttime = d + '天';
this.setData({
memberimgUrls: this.data.memberimgUrls
})
}
var that = this;
counteventtimes[frindex] = setTimeout(function () {
that.countEventTime(endtime, frindex, secindex, status);
}, 1000);
},
onHide: function () {
for (var i = 0; i < this.data.memberimgUrls.length; i++) {
for (var j = 0; j < this.data.memberimgUrls[i].list.length; j++) {
if (this.data.memberimgUrls[i].list[j].status == 'going') {
clearTimeout(this.data.memberimgUrls[i].list[j]);
}
}
}
}
})
是清除方法不对吗
你的代码写得有问题
https://developers.weixin.qq.com/s/dBmlcxma7bhh
我服了你 你是我第二个见过这么写的人 哪有循环加延时调用的 你循环十次 相当于添加了十个timeOut 为啥要这么写呢
你完全可以一个timeOut每隔一秒处理一下整个数组呀
移除只需要移除一个timeOut就行了