Interval and Timer Operator - RXJS
In this tutorial, we will learn about the interval and timer operator of RXJS.
Interval Operator
Interval Operator make calls on some interval
Example:
import { interval } from "rxjs";
------------------------------
const source$= interval(2000);
source$.subscribe((res) => {
console.log(res);
});
Output:
1,2,3,4,5...........
The above example will print the response every two seconds as defined in the interval operator.
Timer Operator
Timer works same as but takes two argument delay, interval.
Syntax:
timer(delay,interval)
Example:
import { timer} from "rxjs";
------------------------------
const source$ = timer(5000, 1000);
source$.subscribe((res) => {
console.log(res);
});
Output:
1,2,3,4,5...........
In Above eample timer will start after five seconds and will run for every seconds untill we stop.