onLoad(option) {
if (option.ids == 1) {
this.PageCur = 'news'
}
this.location()
if (this.userInfo.id) {
this.getNewNums()
this.getMess()
}
},
async getNewNums() {
const dates = {
id: this.userInfo.id
}
const requestPayload = {
wxDate: this.getParams(dates)
}
const retult = await this.$request('/benefit/wx/reviewMessage/getUnCount', this.$FlieHeader,
requestPayload, 'post')
// 如果请求失败,则终止函数执行
if (retult.code !== 200) return
this.updateNums(retult.data.data)
},
async getMess() {
let timer = setInterval(() => {
this.fun(timer)
}, 10000)
},
fun(timer) {
setTimeout(() => {
// 这里ajax 请求的代码片段和判断是否停止定时器
this.getNewNums()
// 如需要停止定时器,只需加入以下:
// clearInterval(timer)
}, 0)
},
为啥要包装一个延时器,由于setInterval不会清除定时器队列,每重复执行1次都会导致定时器叠加,最终卡死网页。 但是setTimeout是自带清除定时器的