簡體   English   中英

如何告訴 RxJS Observable 計時器立即按順序發出下一個

[英]How to tell an RxJS Observable timer to emit the next in sequence immediately

我非常努力地告訴 RxJS 計時器移動到下一個序列,但無濟於事。 有人可以幫我解決這個問題。 我已經在如何做到這一點上發揮了我的智慧。 但沒有管理它。 這是一個代碼示例,解釋了我正在嘗試做什么。 我試過科目。 我有無論我嘗試什么,似乎您都必須以某種方式丟棄訂閱並重新啟動,即使如此? 我在下面代碼中的評論應該解釋我希望實現的目標。

import { Component, OnInit } from '@angular/core';
import { timer } from 'rxjs';
import { takeWhile } from 'rxjs/operators';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
    title = 'testAngular4';
    private condition = false;

  ngOnInit(): void {
    timer(0, 15000).pipe(
      takeWhile((val) => {
      console.log('inside takeWhile', ' val is ', val);
      return val < 30000;
    })).subscribe(value1 => {
      console.log(' subject subscription value is ', value1);
      timer(300, 3000).subscribe((value2) => {
        // Do something here
        console.log(' inside inner timmer subscribe and value is ', value2);
        // How to tell the above timer to emit next value before the 3 seconds
        // have elapsed. This would obviously be based on some condition
        if (this.condition) {
          // Emit the next value in the inner timer sequence above.
          // I mean don't wait any longer - move on. Emit
        }
      });

      // How to tell the above timer to emit next value before the 3 seconds
      // have elapsed. This would obviously be based on some condition
      if (this.condition) {
        // Emit the next value in the outer timer sequence above
        // I mean don't wait any longer - move on. Emit
      }
    });
}

}

如果我正確理解您的問題,您想在 RxJS 中的特定時間間隔后發出一個事件,您可以評估throttleTime。

您可以在他們的文檔中找到類似的示例: https : //rxjs-dev.firebaseapp.com/guide/overview

暫無
暫無

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

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