After all files have finished running, the array this.currentlyRunning
is emptied and its length becomes zero.
if(numberOfFiles === 0) {
clearInterval(this.repeat);
}
I conducted a test using console.log and found that even though the if condition evaluates to true, the interval is not cleared. The interval keeps running unless I include a return statement after clearInterval. This issue only arises when multiple files are submitted for execution, causing the currentlyRunning array to contain more than one object. It occurs specifically at the end, when the array becomes empty.
In the code snippet below, you can observe the implementation of checking file status and the logic for clearing the interval:
export class ...... {
repeat: any;
currentlyRunning = [];
OnInit(){......}
checkFileStatus() {
let index = 0;
this.repeat = setInterval(() => {
let numberOfFiles = this.currentlyRunning.length;
if(numberOfFiles === 0) {
clearInterval(this.repeat);
}
let file_id = {
userFileId: this.currentlyRunning[index].id
}
this.auth.verifyProgress(file_id).subscribe((res: any)=>{
if(res.userFileStatus === "Completed") {
..........
this.currentlyRunning.splice(index, 1);
});
}
index = index + 1;
if(index >= numberOfFiles) {
index = 0;
}
if(this.currentlyRunning.length === 0) {
clearInterval(this.repeat);
return;
}
},(err)=>{
index = index + 1;
if(index >= numberOfFiles) {
index = 0;
}
if(this.currentlyRunning.length === 0) {
clearInterval(this.repeat);
return;
}
});
}, 4000);
}
}