Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda