当用户移动到另一个 window 时,Rxjs 计时器是否停止工作?
Is Rxjs timer stop working when user move to another window?
我正在 Angular 11 上使用 rxjs 计时器设置 60 秒倒计时。当我保持 window 打开时它工作正常。但是,如果我离开(单击同一浏览器的另一个选项卡),计时器会在几秒钟后自行停止。有一次我等了 3 分钟回来,发现还剩 36 秒。这是怎么发生的,有没有办法避免这种情况?
ts文件相关函数:
countdown: number = 59;
countdownMapping: any = {
'=9' : '0#',
'=8' : '0#',
'=7' : '0#',
'=6' : '0#',
'=5' : '0#',
'=4' : '0#',
'=3' : '0#',
'=2' : '0#',
'=1' : '0#',
'=0' : '0#',
'other': '#'
};
resetResendTimer(clicked) {
if (this.countdownActive === false) {
this.countdownActive = true;
if (clicked) {
this.otherFunction();
}
// Trigger endCountdown after the countdown
timer(1000, 1000)
.pipe(
finalize(() => {
this.endCountdown();
}),
takeWhile(() => this.countdown > 0),
tap(() => this.countdown--)
)
.subscribe();
}
html 文件的相关函数:
Resend verification code
<span *ngIf="countdownActive">
(0:{{countdown | i18nPlural: countdownMapping }})
</span>
我在 Chrome 上看到过这种行为,它与 RXJS 无关,我认为这与为提高性能而在后台选项卡上应用的一些限制有关。
每个选项卡都有一个计时器预算,只有当时间预算为非负数时才允许此计时器任务运行。
我正在 Angular 11 上使用 rxjs 计时器设置 60 秒倒计时。当我保持 window 打开时它工作正常。但是,如果我离开(单击同一浏览器的另一个选项卡),计时器会在几秒钟后自行停止。有一次我等了 3 分钟回来,发现还剩 36 秒。这是怎么发生的,有没有办法避免这种情况?
ts文件相关函数:
countdown: number = 59;
countdownMapping: any = {
'=9' : '0#',
'=8' : '0#',
'=7' : '0#',
'=6' : '0#',
'=5' : '0#',
'=4' : '0#',
'=3' : '0#',
'=2' : '0#',
'=1' : '0#',
'=0' : '0#',
'other': '#'
};
resetResendTimer(clicked) {
if (this.countdownActive === false) {
this.countdownActive = true;
if (clicked) {
this.otherFunction();
}
// Trigger endCountdown after the countdown
timer(1000, 1000)
.pipe(
finalize(() => {
this.endCountdown();
}),
takeWhile(() => this.countdown > 0),
tap(() => this.countdown--)
)
.subscribe();
}
html 文件的相关函数:
Resend verification code
<span *ngIf="countdownActive">
(0:{{countdown | i18nPlural: countdownMapping }})
</span>
我在 Chrome 上看到过这种行为,它与 RXJS 无关,我认为这与为提高性能而在后台选项卡上应用的一些限制有关。
每个选项卡都有一个计时器预算,只有当时间预算为非负数时才允许此计时器任务运行。