Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
After setInterval completion execution not exiting the parent function

By taking response of an API and passing it to another function, using the response ID inside a setInterval for checking the status of an operation for implementing progress bar.

For each time sending the ID by API inside the function returns the status at each instant.

Once the status is "SUCESS" or "Failure" I need to exit the function and perform the next step of operations under the function call it is not happening. If status is "PROGRESS" need the loop to me run for 1 minute and exit.

Here I'm my case I'm using clearInterval if the status is "SUCESS" to break the loop.

Why is is not exiting function after the process is completed?

Is there any alternate way to exit the loop after "SUCESS" or "Failure*"

this.userdat.sendData(data).subscribe((res:any)=>{

      this.checkProgress(res.Id);
      
      //DO THIS  -->Not coming here after exceuction
      
      })
checkProgress(id: any) {
    this.StatFlag = false;
    this.interval = setInterval(() => {
      if (!this.StatFlag) {
      
        this.userdat.operationStatus(id).subscribe((statres: any) => {
          switch (statres.status) {
          
            case "PROGRESS":
              this.stepper.selectedIndex = 1;
              break;
            case "SUCESS":
                this.stepper.selectedIndex = 2;
              this.steppershow = false;
              this.StatFlag = true;
              clearInterval(this.interval)
              break;
            case "FAILED":
              this.steppershow = true;
              clearInterval(this.interval)        
              this.StatFlag = true;
              break;
          }
        });
      }
    }, 50000);
<mat-horizontal-stepper *ngIf="showsteps0" #stepper>
 
 <mat-step label="SUBMITTED" >
 </mat-step>
 <mat-step label=" IN progress" >
 </mat-step>
 <mat-step *ngIf="!steppershow" label="Complteted" >
 </mat-step>
 <mat-step label="Failed" *ngIf="steppershow">
 </mat-step>
 </mat-horizontal-stepper>

I have tried creating a new block after the clearInterval and setting flag for clearInterval it doesn't work as expected.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Maybe it a scope issue. In your setInterval, try to redefine the "this".

   this.interval = setInterval(() => {
       this.userdat.operationStatus(id).subscribe((statres: any) => {
          switch (statres.status) {
             const that = this;
             ...
             
             ...   
             that.StatFlag = true;
   }
about 4 years ago · Juan Pablo Isaza Report

0

After the "SUCESS"/"FAILURE" ,unsubscribe the interval works for me.

this.timeInterval = this.intervalVal.subscribe(() => {

 this.userdat.operationStatus(id).subscribe((statres: any) => {
          switch (statres.status) {
             ...
             ...
             this.timeInterval.unsubscribe()
             break;
             
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!