简单地说,beow代码应该获取当前正在运行的文件的进度,该文件在后端得到处理。它做了它应该做的所有事情。但是在最后,当所有的文件都被处理完的时候,这意味着当this.currentlyRunning.length的长度是0之后,我会在控制台的“未定义的索引”中得到一个错误。
let file_id = {
userFileId: this.currentlyRunning[index].id
}
代码一次又一次地为currentlyRunning数组中的所有文件调用get progress api,直到进度达到100%。这就是我使用setInterval的原因。假设在当前运行的列表中有两个文件,每隔4000毫秒就会调用get progress方法,并在视图中更新每个文件的进度。在处理完这两个文件之后,在控制台上抛出错误"index of undefined“
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);
}
发布于 2019-05-16 03:38:10
也许在此之后:
if(numberOfFiles === 0) {
clearInterval(repeat);
return; // <-- stop further processing
}
https://stackoverflow.com/questions/56155406
复制相似问题