先描述一下我遇到的问题:
是一个.catch捕捉错误,点击确定返回上一页的问题
其中用到了计时器
然后,问题就来了,点击返回之后,计时器停止不了,哭泣
showCourses() {
getCommondityList(
this.page,
this.pageSize,
changeSchoolRegionId,
this.industryId
).then(res => {
this.pageTotal = res.data.pageTotal;
this.courseList = res.data.list;
}).catch(err => {
this.countNumber++;
if (this.countNumber >= 3) {
wx.showModal({
title: '提示',
content: '网络异常,请稍后重试',
showCancel: false,
success(res) {
if (res.confirm) {
wx.navigateBack({
delta: 1
});
}
}
})
} else {
setTimeout(() => {
this.showCourses();
}, 1000);
}
});
}
然后我想到了生命周期,onUnload
停不下来的原因是因为计时器的问题,然后我在页面返回之后,清除了计时器的时间
先赋值
this.timer = setTimeout(() => {
this.showCourses();
}, 1000);
然后在onUnload() { clearTimeout(this.timer); }
也就是页面跳转,销毁本页面内容的时候,清除计时器,然后就可以了,哭泣