![](/img/trans.png)
[英]How do I stop DIV elements from moving when another opens? (menu and submenu)
[英]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 运算符,并像建议的评论一样,在弹出可用时将标志设置为 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 ,它显示了这个想法
用户takeUntill
和repeatWhen
来实现这一点。 如果您使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.