当用户打开弹出窗口时如何停止轮询?

How do i stop polling when user opens a popup?

 update(){
    this.timeInterval = interval(2000).pipe(startWith(0), switchMap(() => this.deviceService.getDeviceList())
    ).subscribe((success: any) => {
      this.rowData = success;
      console.log("hello")
    },
    retry(2)
    );
  }

我有这段代码,每 2 秒获取一次详细信息。但我想在用户打开任何弹出窗口时暂停此方法,并在他关闭弹出窗口后再次启动它。不知道如何在 angular?

中实现此目标

您可以使用 skipWhile rxjs 运算符,并像建议的评论一样,在 pop 可用时将标志设置为 true,然后在不可用时将其删除:

popOpen = false;

update(){
  this.timeInterval = interval(2000)
   .pipe(startWith(0), 
         skipWhile(() => this.popupOpen),
         switchMap(() => this.deviceService.getDeviceList())
  ).subscribe((success: any) => {
    this.rowData = success;
    console.log("hello")
  },
  retry(2)
);
}

看到这个 stackblitz 展示了这个想法

用户 takeUntillrepeatWhen 实现了这一点。如果你用 this.flag$.next(false) 调用 flag$ 它将停止。要恢复,您需要拨打 this.flag$.next(true)。查看 stackblitz 以获得更多信息。

flag$: Subject<boolean> = new Subject<boolean>();

this.timeInterval = interval(2000)
      .pipe(
        startWith(0),
        switchMap(() => this.getUsers())
      )
      .pipe(
        takeUntil(this.flag$),
        repeatWhen(() => this.flag$)
      )
      .subscribe((success: any) => {
        console.log(success);
      }, retry(2));

这里是 stackblitz 示例:https://stackblitz.com/edit/angular-ivy-cisbks?file=src/app/app.component.ts