Rob*_*ann 4 timer rxjs angular switchmap
所以我是 RXJS 的新手。我想要做的是设置一个会话到期计时器,当用户收到他们的会话即将到期的模式提示时,如果他们单击继续,计时器将被重置。
我一直在阅读 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);
});
}
Run Code Online (Sandbox Code Playgroud)
在我的代码的其他地方,我订阅了 tokenExpirationTime(这是我如何知道计时器上的当前时间,所以我知道何时显示我的弹出窗口)。
前任。
this.user.tokenExpirationTime.subscribe(x => { ... });
Run Code Online (Sandbox Code Playgroud)
我可能做错了这一切,因为我是新手。我希望我的解释清楚,但如果没有,请告诉我。感谢您的帮助!
代替
timer(0, 1000).pipe(
// … all the other stuff …
)
Run Code Online (Sandbox Code Playgroud)
和
// 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 …
)
Run Code Online (Sandbox Code Playgroud)
在哪里
private reset$ = new Subject<void>();
Run Code Online (Sandbox Code Playgroud)
然后你可以添加一个函数
public resetTimer() {
this.reset$.next();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3516 次 |
| 最近记录: |