Angular 带时间间隔的条件轮询

Angular conditional polling with time interval

我有一个像下面这样的轮询场景,我每 1 秒轮询一次休息 API 并等待结果

interval(1000)
      .pipe(
        startWith(0),
        switchMap(() => this.itemService.getItems(shopId))
      )
      .subscribe(response => {
        console.log(response);
        
        if(response && response.status = false) {
            // stop polling
        }
        
        );
      });

投票部分工作正常。问题是我希望在收到响应且其状态为 false 时停止轮询。如何更改此代码以有条件地停止轮询?

你可以用takeWhile

 Pool()
    {
      interval(1000)
      .pipe(
        startWith(0),
        switchMap(() => this.itemService.getItems(shopId)),
        takeWhile(response=> response.status == true) //takeWhile(response=> response.status)
      )
      .subscribe(response => {
    
      });

}