简体   繁体   English

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

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

I have tried very hard to tell a RxJS timer to move onto the next sequence to no avail.我非常努力地告诉 RxJS 计时器移动到下一个序列,但无济于事。 Could someone please help me out with this one.有人可以帮我解决这个问题。 I have busted my wits on how to do this.我已经在如何做到这一点上发挥了我的智慧。 But have not managed it.但没有管理它。 Here is a sample of code explaining what I am trying to do.这是一个代码示例,解释了我正在尝试做什么。 I have tried Subjects.我试过科目。 I have No matter what I try it seems that you have to somehow throw the subscription away and restart, Even then how??我有无论我尝试什么,似乎您都必须以某种方式丢弃订阅并重新启动,即使如此? My comments in the code below should explain what I wish to achieve.我在下面代码中的评论应该解释我希望实现的目标。

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
      }
    });
}

} }

If I understood your problem correctly, you want to emit an event after certain time interval in RxJS, you can evaluate throttleTime.如果我正确理解您的问题,您想在 RxJS 中的特定时间间隔后发出一个事件,您可以评估throttleTime。

You can find a similar example in their documentation: https://rxjs-dev.firebaseapp.com/guide/overview您可以在他们的文档中找到类似的示例: https : //rxjs-dev.firebaseapp.com/guide/overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM