簡體   English   中英

在條件下重置 Angular RXJS 計時器

[英]Reset Angular RXJS Timer On condition

我有以下代碼

    public timePast:Observable<number>;
    private readonly _stop = new Subject<void>();
    private readonly _start = new Subject<void>();

    this.timePast = timer(1000, 1000).pipe(
            takeUntil(this._stop),
            repeatWhen(() => this._start)
        );

    this.timePast.subscribe(val =>
          {

            console.log(val);
              if(val>=sometime)
              {
                //reset timer
              }

          } );

start(): void {
    this._start.next();
  }
  stop(): void {
    this._stop.next();
  }

如何重置計時器?

您可以反轉開始的部分邏輯:

public timePast: Observable<number> = this._start.pipe(
  switchMap(() => timer(1000, 1000).pipe(takeUntil(this._stop)))
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM