RxJS Interval without delay
Asked Answered
S

1

11

The following code emits an int after 5000ms and then another in every 5000ms later:

let evens = Observable.interval(5000)
     .map(i => {
          return i * 2;
      });

 evens.subscribe((i) => {
      console.log(i);
 });

Is it possible to do this, but have the first result immediately (0ms), and then wait 5000ms between subsequent results?

Slumberland answered 7/3, 2017 at 11:49 Comment(0)
S
18

You can use timer() where the first parameter is the initial delay:

timer(0, 5000);

Or if you want the first item to be emitted immediately you can also use the startWith() operator.

Jan 2019: Updated for RxJS 6

Sitar answered 7/3, 2017 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.