Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当用户打开弹出窗口时,如何停止轮询?_Javascript_Angular_Typescript_Angular Material_Polling - Fatal编程技术网

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

Javascript 当用户打开弹出窗口时,如何停止轮询?,javascript,angular,typescript,angular-material,polling,Javascript,Angular,Typescript,Angular Material,Polling,我有这段代码,每2秒后获取详细信息。但我想在用户打开任何弹出窗口时暂停此方法,并在关闭弹出窗口后再次启动它。不明白如何在angular中实现这一点?您可以使用skipWhilerxjs操作符,并按照建议的注释,在pop可用时将标志设置为true,然后在其不可用时将其删除: update(){ this.timeInterval = interval(2000).pipe(startWith(0), switchMap(() => this.deviceService.getDev

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

您可以使用
skipWhile
rxjs操作符,并按照建议的注释,在pop可用时将标志设置为true,然后在其不可用时将其删除:

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

请参见此图,其中显示了想法

用户
takeUntill
repeatWhen
,以实现此目的。如果使用
this.flag$.next(false)
调用标志$,它将停止。要继续,您需要使用
此.flag$.next(true)
调用。查看stackblitz以了解更多信息

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)
);
}
flag$:Subject=newsubject();
this.timeInterval=interval(2000)
.烟斗(
startWith(0),
switchMap(()=>this.getUsers())
)
.烟斗(
takeUntil(this.flag$),
repeatWhen(()=>此.flag$)
)
.订阅((成功:任何)=>{
console.log(成功);
},重试(2));

这里的stackblitz示例:

只需添加一个标志类型变量。如果弹出窗口打开,则将其值设置为1,并添加一个条件,即如果标志类型变量为0,则调用函数,否则不调用。感谢我尝试了此操作,我创建了一个全局变量,并在对话框窗口中将其更改为false,当其关闭时再次设置为true,但似乎不起作用。它不应该是全局变量,应该是进行http调用的组件服务中的一个变量,如果无法从弹出窗口更改它,请将逻辑更改为
deviceService.GetDeviceList()
内部。请详细说明一下好吗?是的,在
deviceService
上公开函数setPopupOpen(val:boolean),然后,skipWhile应该更改为
skipWhile(()=>this.deviceService.isPopupOpen)
。打开弹出窗口时,调用
this.deviceService.setPopupOpen(true)
(关闭弹出窗口时使用false),
isPopupPen
应该是在
deviceService
上公开的只读属性(因此只有服务会更改实际的私有属性)
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));