1

Run method every 5 seconds related on status

I have method in component that getting data from back end and check statuses

Here is it

 getRecognitionById() {
    this.loaderService.show(null, true);

    this.vendorWebApiService
      .createRecognition(this.executiveChangeId)
      .pipe(take(1))
      .subscribe((res) => {
        this.vendorWebApiService
          .getRecognition(res.taskRequestId, this.executiveChangeId)
          .pipe(take(1))
          .subscribe((recognitionResponse) => {
            if (recognitionResponse.jobStatus === "completed") {
              this.recognitionData = recognitionResponse;
              this.getLatesFeedback();
            }
            if (recognitionResponse.jobStatus === "failed") {
              alert();
            } else {
              
            }
          });
      });
  }

In this part I check status

 this.vendorWebApiService
      .getRecognition(res.taskRequestId, this.executiveChangeId)
      .pipe(take(1))
      .subscribe((recognitionResponse) => {
        if (recognitionResponse.jobStatus === "completed") {
          this.recognitionData = recognitionResponse;
          this.getLatesFeedback();
        }
        if (recognitionResponse.jobStatus === "failed") {
          alert();
        } else {

        }
      });

But problem that if status is another then complete or failed, I need to rerun this logic again every 5 seconds , so every 5 seconds I need to check status and after 10's try, I need to show alert.

How I need to rewrite my code to achieve this logic?

Submitted October 12th 2021 by Admin

Answers
0

You can do this with rxjs

 import { interval, Subject, Subscription } from 'rxjs'; refresher$: Observable<number>; refreshSub: Subscription; checkStatus() { this.vendorWebApiService .getRecognition(res.taskRequestId, this.executiveChangeId) .pipe(take(1)) .subscribe((recognitionResponse) => { if (recognitionResponse.jobStatus === "completed") { this.recognitionData = recognitionResponse; this.getLatesFeedback(); } if (recognitionResponse.jobStatus === "failed") { alert(); } else { } }); } getRecognitionById() { this.loaderService.show(null, true); this.checkStatus() } this.refresher$ = interval(5000); // every5 sec this.refreshSub = this.refresher$.subscribe(() => { this.checkStatus() }); 

Admin | 6 days ago


0

You can achieve that this way:

  1. Define a counter variable.

  2. Define an interval with a 5000ms timer and reference it to a variable.

  3. Clear interval on success.

  4. Re-run interval on failure and a counter of counter < 10.

let counter = 0;
let interval = setInterval(() => { // ajax().next(() => { // clearInterval(interval); // }).catch(() => { // if (counter >= 10) { // clearInterval(interval); // } else { // counter++; // } // })
}, 5000);
  • Do NOT forget to clear your interval in ngOnDestroy to prevent your app from crash in some scenarios.

Admin | 6 days ago


0

With observables you could try something like this

export function getRecognitionById() { this.loaderService.show(null, true); const ATTEMPT_COUNT = 10; const DELAY = 5000; this.vendorWebApiService .createRecognition(this.executiveChangeId) .pipe(take(1)) .switchmap((res) => { return this.vendorWebApiService .getRecognition(res.taskRequestId, this.executiveChangeId) .pipe(take(1)) }).map((recognitionResponse) => { if (recognitionResponse.jobStatus === "completed") { this.recognitionData = recognitionResponse; this.getLatesFeedback(); } if (recognitionResponse.jobStatus === "failed") { alert(); } else { throw { error: 'failed' } } }).retryWhen(errors => errors.pipe( scan((errorCount, err: any) => { if (err.error === 'failed' || errorCount >= ATTEMPT_COUNT) { throw err; } return errorCount + 1; }, 0), delay(DELAY), ));

Admin | 6 days ago



Relevant Questions