Angular 6 - run method in service every 10 seconds

前端 未结 7 1955
难免孤独
难免孤独 2020-12-14 02:52

I have this service using HttpClient to get some data :

checkData() {
    return this.http.get(\'my url\');
}

The on the footer component I

7条回答
  •  轮回少年
    2020-12-14 03:43

    Hope this will help you

    export class YourComponent implements OnInit, OnDestroy {
      private alive: boolean;
    
      constructor(){}
    
      ngOnInit(){
        this.alive = true;
        TimerObservable.create(0, 10000)
          .pipe(
            takeWhile(() => this.alive)
          )
          .subscribe(() => {
            this.myservice.checkdata().subscribe( result => { this.statustext = result } );
          });
      }
    
      ngOnDestroy(){
        this.alive = false;
      }
    }
    

提交回复
热议问题