Angular 6 - run method in service every 10 seconds

前端 未结 7 1952
难免孤独
难免孤独 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:50

    I am referring to angular 8:

    Please note the difference between timer and interval.

    You would use the timer if you want to delay a single function call but you want to use an invertal if you want to fire multiple function calls in sequence with a delay between: http://tutorials.jenkov.com/angularjs/timeout-interval.html

    I found the following code snipped in this blog post: http://tutorials.jenkov.com/angularjs/timeout-interval.html

    ngOnInit() {
        interval(5000)
          .pipe(
            startWith(0),
            switchMap(() => this.apiService.getTweets())
          )
          .subscribe(res => this.statuses = res.statuses})
        ;
      }
    
    0 讨论(0)
提交回复
热议问题