[英]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.用户
takeUntill
和repeatWhen
来实现这一点。 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.