import { timer } from 'rxjs'; //emit value after 1 second const source = timer(1000); //output: 0 const subscribe = source.subscribe(val => console.log(val));
import { timer } from 'rxjs'; //emit value in interval of 1 second const source = timer(0, 1000); //output: 0,1,2,3,4,5...... const subscribe = source.subscribe(val => console.log(val));
import { timer } from 'rxjs'; //stop timer after 5 seconds const source = timer(0, 1000).pipe(takeUntil(timer(5000))); //output: 0,1,2,3,4 const subscribe = source.subscribe(val => console.log(val));This example creates an observable that emits values in an interval of 1 second after an initial delay of 0 seconds and stops after 5 seconds using the takeUntil operator. The emitted values are then logged to the console. Overall, the RxJS library provides multiple useful operators and functions for asynchronous programming using observables in TypeScript.