简体   繁体   English

带有间隔/计时器的可观察合并最新

[英]Observable combineLatest with interval/timer

I'm trying to use .combineLatest in conjunction with a set interval so that a new value is emitted every 5 seconds. 我正在尝试将.combineLatest与设置的间隔结合使用,以便每5秒发出一个新值。 I am then passing this.dailyStocks as an input into a child component but the data being passed into the input is the same every time and when I look at my network tab in dev tools, I do not see the additional calls being made. 然后,我将this.dailyStocks作为输入传递给子组件,但是每次传递到输入的数据都是相同的,当我在开发工具中查看“网络”选项卡时,看不到正在进行的其他调用。

  .flatMap(() => {
    return Observable.interval(5000).combineLatest(
      this.http.getData(args),
      this.http.getOtherData(args)
    )
  })
  .subscribe(res => {
    this.dailyStocks = res[1];
    this.dailyStocks = this.dailyStocks.slice(0)
  }

Child Component: 子组件:

  @Input('dailyStocks')
  set dailyStocks(val: ListSymbolObj[]) {
    this._dailyStocks.next(val);
  }

  get dailyStocks() {
    return this._dailyStocks.getValue();
  }

  ngOnInit() {
    this._dailyStocks
          .filter(x => x != undefined)
          .take(1)
          .subscribe(res => {
            //res is same
          }
    }

To make request on every value from interval you should write it like this: 要对间隔中的每个值发出请求,您应该这样编写:

Observable.interval(5000).switchMap(() =>
  Observable.combineLatest(
    this.http.getData(args),
    this.http.getOtherData(args)      
  )
)

Currently, your code not making additional requests because it just combines together values from all three Observables. 当前,您的代码没有发出其他请求,因为它只是将所有三个Observable的值组合在一起。 It is equivalent to the following code: 它等效于以下代码:

Observable.combineLatest(
  Observable.interval(5000),
  this.http.getData(args),
  this.http.getOtherData(args)      
)

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

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