gpt4 book ai didi

angular - RXJS - 用于重置计时器的 SwitchMap 或 SwitchMapTo

转载 作者:太空狗 更新时间:2023-10-29 18:30:12 27 4
gpt4 key购买 nike

所以我是 RXJS 的新手。我想做的是设置一个 session 到期计时器,当用户收到他们的 session 即将到期的模态提示时,如果他们单击继续,计时器将重置。

我一直在阅读 switchMap 和 switchMapTo,但我发现的示例使用了某种单击事件或鼠标移动事件。我的情况是,我不想在单击按钮时刷新,因为我正在验证 JWT。 JWT 验证成功后,我将刷新计时器。

我有一个用户通用库,可以像这样设置计时器:

private tick: Subscription;
public tokenExpirationTime: Subject<number>;

setupExpirationTimer():void {
// Start the timer based on the expiration
var expirationSeconds = this.expiration * 60;
this.tick = Observable.timer(0, 1000).map(i => expirationSeconds - i).subscribe(x => {
// Set the seconds in the user object
this.tokenExpirationTime.next(x);
console.log("TIMER: " + x);
});
}

在我的代码的其他地方,我订阅了 tokenExpirationTime(这是我知道计时器上的当前时间的方式,所以我知道何时显示我的弹出窗口)。

例如

this.user.tokenExpirationTime.subscribe(x => { ... });

我可能做错了,因为我是新手。我希望我的解释很清楚,但如果不明白请告诉我。感谢您的帮助!

最佳答案

替换

timer(0, 1000).pipe(
// … all the other stuff …
)

// Whenever reset$ emits…
reset$.pipe(
// (but we emit once initially to get the timer going)
startWith(undefined as void),
// … start a new timer
switchMap(() => timer(0, 1000)),
// … all the other stuff …
)

在哪里

private reset$ = new Subject<void>();

然后你可以添加一个函数,比如

public resetTimer() {
this.reset$.next();
}

关于angular - RXJS - 用于重置计时器的 SwitchMap 或 SwitchMapTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48010466/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com