javascriptrxjsrxjs-observables

rxjs emit only at specific intervals


I would like to call a function only at specific intervals of time, specifically 3 and 6 seconds, and am having some difficulty finding the correct operator to use.

bufferTime seemed like a good candidate, but the array it outputs isn't what I would expect.

Here's a short example:

summarize(): void {
  const responseInterval = interval(1000);
  const responseIntervalObservable = responseInterval.pipe(bufferTime(1000));

  responseIntervalObservable.subscribe(intervalValue => {
    console.log('intervalValue', intervalValue);
  });
}

This outputs:

intervalValue []
intervalValue [0, 1]
intervalValue [2]
intervalValue [3]
intervalValue [4]
intervalValue [5]
intervalValue [6]
intervalValue [7]
intervalValue [8]

I was hoping to see something like [1], [2], [3] so I could call a function specifically at 3 seconds and again at 6 seconds.

What is the correct, or better operator to use?


Solution

  • I ended up just using interval rather than putting bufferTime in-bewteen. That emits a zero-based index for the iteration rather than an array.

    Then I can just check the index for a specific value until the request is complete or times out.