uniapp 在定时器中赋值下载文件进度导致进度 混乱
页面逻辑就是,在下载页面点击下载之后,开始执行下载,如果用户退出了这个页面,再进入这个页面还是能正常显示 按钮上面的百分比,
我做了一个操作就是,在下载方法里面,将进度的百分比赋值给了,按钮上的参数,保存在了浏览器中,确实退出页面之后再能够取到这个百分比,但是这个值不是动态的,
所以我就想把赋值 存值这一步放进setInterval 定时器里面,就导致progress 下载进度数据混乱变成 0 1 8 5 0 1 7 0 不规则的进度了
// 点击方法
onClick() {
console.log(1)
const downloadTask = uni.downloadFile({
url: '下载文件地址',
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
uni.showToast({
title: "下载成功",
icon: "success",
})
console.log('安装包下载成功,即将安装:' + JSON.stringify(downloadResult, null, 4));
// install openFile
plus.runtime.install(downloadResult.tempFilePath);
} else {
console.log(downloadResult.statusCode)
uni.showToast({
title: "下载失败,联系管理官",
})
}
}
});
//下载进度
downloadTask.onProgressUpdate(res => {
// console.log('下载进度百分比:' + res.progress); // 下载进度百分比
const timer = setInterval(() => {
this.downloadPercent = res.progress; //
uni.setStorageSync('time', this.downloadPercent)
if (this.downloadPercent === 100) {
clearInterval(timer)
uni.removeStorageSync('time')
} else{
console.log(this.downloadPercent,'else' )
}
}, 1000)
});
},
//onshow生命周期
onShow() {
let time = uni.getStorageSync('time') // 获取值
console.log('onshow', this.downloadPercent)
if (time > 0) {
//赋值
this.downloadPercent = time
console.log(this.downloadPercent, 'onshow')
}
},