简单来说,beow代码应该获取当前正在运行的文件的进度,该文件在后端进行处理。它完成了它应该做的所有事情。但是最后处理完所有文件后,这意味着this.currentlyRunning.length的长度为0之后,我在此行的控制台“未定义索引”中得到了错误
let file_id = {
userFileId: this.currentlyRunning[index].id
}
代码会反复调用currentRunning数组中所有文件的api get进度,直到进度达到100%。这就是为什么我使用setInterval的原因。 可以说“当前正在运行”列表中有两个文件,每隔4000毫秒将调用get progress方法,并且在视图中更新每个文件的进度。在处理完两个文件之后,错误将在控制台“未定义的索引”上引发
checkProgress() {
let index = 0;
let repeat = setInterval(() => {
let numberOfFiles = this.currentlyRunning.length;
if(numberOfFiles === 0) {
clearInterval(repeat);
}
let file_id = {
userFileId: this.currentlyRunning[index].id
}
this.auth.getProgress(file_id).subscribe((res: any)=>{
this.currentlyRunning[index]['progress'] = Math.round(res.percent);
let indexOfFileHistory = this.uploadedFileHistory.findIndex((file: any)=> file.id === this.currentlyRunning[index].id);
this.uploadedFileHistory[indexOfFileHistory] = this.currentlyRunning[index];
if(this.currentlyRunning[index].progress === 100 || res.status == "badBounceRate") {
//the origial array which contains all the files we are just updating its status on 100% individual file object is returned with updated status
this.uploadedFileHistory[indexOfFileHistory] = res.data;
//removing the file from currently running
this.currentlyRunning.splice(index, 1);
}
//incrementing so that next time it runs for the other file
index = index + 1;
if(index >= numberOfFiles) {
index = 0;
}
let running = this.uploadedFileHistory.find((file: any)=>file.status === "Running");
if(!running) {
clearInterval(repeat);
}
},(err)=>{
//want to run it regardless of the error
index = index + 1;
if(index >= numberOfFiles) {
index = 0;
}
let running = this.uploadedFileHistory.find((file: any)=>file.status === "Running");
if(!running) {
clearInterval(repeat);
}
});
}, 4000);
}
答案 0 :(得分:0)
也许之后:
if(numberOfFiles === 0) {
clearInterval(repeat);
return; // <-- stop further processing
}