emit numbers in sequence every specified duration using interval and timer in RxJS
interval
// RxJS v6+ import { interval } from 'rxjs'; //emit value in sequence every 1 second const source = interval(1000); //output: 0,1,2,3,4,5.... const subscribe = source.subscribe(val => console.log(val));
timer
// RxJS v6+ import { timer } from 'rxjs'; /* timer takes a second argument, how often to emit subsequent values in this case we will emit first value after 1 second and subsequent values every 2 seconds after */ const source = timer(1000, 2000); //output: 0,1,2,3,4,5...... const subscribe = source.subscribe(val => console.log(val));
// RxJS v6+ import { timer } from 'rxjs'; //emit 0 after 1 second then complete, since no second argument is supplied const source = timer(1000); //output: 0 const subscribe = source.subscribe(val => console.log(val));
References
https://stackoverflow.com/questions/44165893/how-do-i-make-an-observable-interval-start-immediately-without-a-delay
https://www.learnrxjs.io/operators/creation/interval.html
https://www.learnrxjs.io/operators/creation/timer.html
https://rxjs.dev/api/index/function/interval
https://rxjs.dev/api/index/function/timer
https://stackoverflow.com/questions/44165893/how-do-i-make-an-observable-interval-start-immediately-without-a-delay