Interval and Timer - RXJSOperator

Interval and Timer -  RXJSOperator

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.

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

You might also like

Leave a Comment