简体   繁体   English

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

[英]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)
    );
  }

I have this code which fetches the details after every 2sec.我有这段代码,它每 2 秒获取一次详细信息。 but i want to pause this method whenever user opens up any popup and again start it once he closes the pop.但是我想在用户打开任何弹出窗口时暂停此方法,并在关闭弹出窗口后再次启动它。 Not getting how can i achieve this in angular?不明白如何在 angular 中实现这一点?

You can use the skipWhile rxjs operator, and like the comment suggested, set the flag to true when a pop is available, then remove it when its not:您可以使用skipWhile rxjs 运算符,并像建议的评论一样,在弹出可用时将标志设置为 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)
);
}

see this stackblitz which shows the idea看到这个stackblitz ,它显示了这个想法

User takeUntill and repeatWhen for acheive this.用户takeUntillrepeatWhen来实现这一点。 If you call the flag$ with the this.flag$.next(false) it will stop.如果您使用this.flag$.next(false)调用 flag$,它将停止。 To resume you need to call with this.flag$.next(true) .要恢复,您需要使用this.flag$.next(true)调用。 check the stackblitz for more clearence.检查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));

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

暂无
暂无

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

相关问题 当另一个打开时,如何阻止DIV元素移动? (菜单和子菜单) - How do I stop DIV elements from moving when another opens? (menu and submenu) 我怎么知道弹出 window 何时打开? - How can I know when a popup window opens? 如果用户已登录FB,如何阻止我的Facebook登录弹出窗口出现? - How do I stop my Facebook login popup from appearing if user is already logged into FB? 如何停止Javascript执行? 还有所有的计时器? (如何从轮询中停止函数 - 或者将其进行monkeypatching) - How do I stop Javascript execution? And all timers? (How to stop a function from polling - or monkeypatching it) 子框架打开一个弹出窗口。 如何检查是否从父级打开了该弹出窗口? - Child frame opens a popup. How do I check if that popup is opened from the parent? 当用户在新窗口中打开时,如何使onclick“ window.location”也起作用 - javascript - How do I make onclick“window.location” also work when user opens in new window 弹出警报提示,如何阻止它循环播放? - Popup Alert looops, how do i stop it to loop? 警报框打开时如何停止加载程序 - How to stop loader when alert box opens 如何创建导航栏,单击该导航栏会在同一页面上将其打开? - How do I creat a navbar that when clicked it opens on the same page? 我正在使用弹出窗口进行用户登录,当用户实际登录时如何刷新父(打开)页面? - I'm using a popup for user login, how do i refresh the parent(openner) page when user actually logs in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM