简体   繁体   English

Angular 有时间间隔的条件轮询

[英]Angular conditional polling with time interval

I have a polling scenario like below where I'm polling every 1 secs to a rest API and waits for a result我有一个像下面这样的轮询场景,我每 1 秒轮询一次 rest API 并等待结果

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

polling part is working without an issue.投票部分工作没有问题。 The issue is I want the polling to stop when I get a response and its status is false.问题是我希望在收到响应并且其状态为 false 时停止轮询。 How to change this code to conditionally stop polling?如何更改此代码以有条件地停止轮询?

U can use takeWhile你可以使用takeWhile

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM