如何告诉RxJS Observable计时器立即按顺序发出下一个

时间:2018-09-18 22:06:44

标签: angular rxjs observable

我已经非常努力地告诉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
      }
    });
}

}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您希望在RxJS中的某个时间间隔后发出一个事件,则可以计算一下油门时间。

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