4

I am new to rxjs and was playing around with them. So here's the code that emits values every second.And since it passes through the delay operator which is set for 10 secs, I was expecting the output to be consoled every 11 secs,

import { of,interval } from 'rxjs'; 
import { map,delay,timeInterval } from 'rxjs/operators';


interval(1000)
.pipe(delay(10000))
.pipe(timeInterval())
.subscribe(val=>{
  console.log(val);
});

Which was the case the first time. And after that it started consoling output every second instead of 11 secs. How does this happen? Here's the output for your reference.

TimeInterval {value: 0, interval: 11004}
TimeInterval {value: 1, interval: 997}
TimeInterval {value: 2, interval: 1000}
TimeInterval {value: 3, interval: 1003}
TimeInterval {value: 4, interval: 997}
TimeInterval {value: 5, interval: 1000}
...
Emile Rivere
  • 107
  • 1
  • 6
  • 2
    Say you have a bunch of bowling balls and you're at the top of a building. You drop one off the balcony every second `interval`. You're high enough up that they take ten seconds to hit the ground `pipe(delay(10000)).` The interval `TimeInterval` between the bowling balls hitting the ground would still be one second. – Tanner Jan 17 '20 at 15:01
  • @Tanner You should put your comment into an answer, I feel like you deserve credit for the clear and succint explanation :) – Ashley Jan 17 '20 at 15:25
  • [delay](https://rxjs-dev.firebaseapp.com/api/operators/delay) `If the delay argument is a Number, this operator time shifts the source Observable by that amount of time expressed in milliseconds. The relative time intervals between the values are preserved.` – frido Jan 18 '20 at 11:10
  • @fridoo great! Just move it as an answer and I would be happy to accept – Emile Rivere Jan 18 '20 at 13:58

1 Answers1

0

The output is correct. The whole stream is time shifted by delay.

If the delay argument is a Number, this operator time shifts the source Observable by that amount of time expressed in milliseconds. The relative time intervals between the values are preserved.
[RxJS - delay]

To wait a given amount of time after every item see:

Delay for every element with RXJS

Guarantee n seconds between emit without waiting initially

frido
  • 13,065
  • 5
  • 42
  • 56