【问题标题】:Reset the Observable Timer on user action?重置用户操作的可观察计时器?
【发布时间】:2018-08-23 06:33:56
【问题描述】:

在我的 Angular 项目中,我需要在令牌过期之前通过用户交互来刷新它。在 UI 上,我们会在会话过期前 5 分钟显示会话超时消息和更新图标,当用户单击更新图标时,我们会刷新令牌,现在它会再次使用这个新令牌检查会话,依此类推。

  • RxJs v 5.1

  • Angular v 4.0

我使用的是setInterval()clearInterval(),这里是相关代码

@Compononet({})
export class AdminLayoutComponent implements OnInit {
    isRefreshingToken: boolean;
    interval: any;
    period: number;
    constructor() {
        this.notifyMinutes = 5; // in minutes
        this.isRefreshingToken = false;
        this.period = 60 * 1000;
        this.timer$ = Observable.timer(0, this.period);
    }

    ngOninit() {
        this.interval = <any>setInterval(() => this.checkSession(), this.period);
    }

    private checkSession() {
        // calculate the remaining time and display the message accordingly
        if (remainingTime < 5 minutes) {
            this.refreshTokenMethod.finally(() => {
                this.isRefreshingToken = false;
            }).subscribe() {
                this.isRefreshingToken = true;
            }
        }
    }
    ngOnDestroy() {
        if (this.interval) {
            clearInterval(this.interval);
        }

    }
}

这很好,但我认为 Observables 是实现相同目标的更好工具。所以尝试了这种方式

export class AdminLayoutComponent implements OnInit {

    timer$: Observable<any>;
    subscription: Subscription;

    constructor() {
        this.timer$ = Observable.timer(0, this.period);
    }

    ngOnInit() {
        this.subscription = this.timer$.subscribe((i) => {
            console.log('timer: ', i);
            this.checkSession();
        });
    }

    ngOnDestroy() {
        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    }
}

现在的问题是当用户点击更新图标时;我想再次重置计时器以检查时间。

我该怎么做?或者可以运行计时器吗?

【问题讨论】:

    标签: angular observable rxjs5


    【解决方案1】:

    您需要使用 Subject Observable。使用 next() 重置计时器。

    export class AdminLayoutComponent implements OnInit {
    
        private reset$ = new Subject();
        timer$: Observable<any>;
        subscription: Subscription;
    
        constructor() {
            this.timer$ = this.reset$.pipe(
              startWith(0),
              switchMap(() => timer(0, 30000))
            );
        }
    
        ngOnInit() {
            this.subscription = this.timer$.subscribe((i) => {
                console.log('timer: ', i);
                this.checkSession();
            });
        }
    
       refreshTimer(): void {
          this.reset$.next(void 0);
        }
    
        ngOnDestroy() {
            if (this.subscription) {
                this.subscription.unsubscribe();
            }
        }
    }
    

    还更新了 stackblitz 代码https://stackblitz.com/edit/angular-pnwh44

    【讨论】:

    • 感谢您的回复,但我如何验证您的实施。您提供的演示无法检查计时器是否重置。
    • 可以参考console.log。默认情况下,将打印计时器 0、计时器 1、计时器 2。单击“刷新”按钮时,将打印计时器 0。
    • this.timer$ = this.reset$.pipe( startWith(0), switchMap(() =&gt; timer(0, 30000)) ); 我们如何使用 . 以 concat 方式更改它我没有使用 .pipe
    • 你的 RxJS 版本是多少?
    • RxJS v 5.1。我没有在任何地方使用过pipe,所以为了代码的一致性不想使用。这就是原因。
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多