0

I have an Angular7 client application which consumes a webAPI. I want to call a webAPI method again and again, and I want to display the output of that API method call on UI on each call.

If I do this using a while loop I won't be able to display continuously and the application will get hang. So decided to do this with thread but I don't know how to implement thread in TypeScript.

Ralf
  • 16,086
  • 4
  • 44
  • 68
Nagaraj
  • 45
  • 4
  • Possible duplicate of [Use thread in Angular 5](https://stackoverflow.com/questions/51237613/use-thread-in-angular-5) – MrPromethee Jun 05 '19 at 12:06
  • You should be using the rxjs library and using Subsriptions. Have a look here: https://angular.io/guide/rx-library – Jinish Jun 05 '19 at 13:35

1 Answers1

0

You could use setInterval() to call the api repeatedly and clearInterval() to stop it.For example,

startTimer() {
  this.interval = setInterval(() => {
    this.http.get<WeatherForecast[]>('https://localhost:44360/api/SampleData/WeatherForecasts').subscribe(result => {
      //do things
    }, error => console.error(error));
}, 1000)//repeat every sceond

pauseTimer() {
    clearInterval(this.interval);
}

Refer to How to do a timer in Angular 5, Angular 6 - run method is service every 10 seconds

Ryan
  • 19,118
  • 10
  • 37
  • 53