How to make an http call every 2 minutes with RXJS?

前端 未结 5 406
猫巷女王i
猫巷女王i 2020-12-03 05:14

I have a service that will make a call to my rest service every 2 minutes. On my service I have the following function

  getNotifications(token: string) {
           


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 06:09

    I had a similar need.can be useful to someone and hence writing it here.My Angular version is 9.1.5. I am checking if the user is logged in and sending a http request every 10 minutes until the user is in that component.

     const secondsCounter = interval(60000); //Refreshes every 10 minutes
     secondsCounter
     .pipe(
       tap(console.log),
       takeWhile(x => this.notificationService.isLoggedIn()),
       flatMap(() => this.notificationService.getNotifications(this.token))
     ).subscribe()
    

提交回复
热议问题