小编Rob*_*ann的帖子

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

所以我是 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 rxjs angular switchmap

4
推荐指数
1
解决办法
3516
查看次数

标签 统计

angular ×1

rxjs ×1

switchmap ×1

timer ×1