Contents

interval operator in RxJs

Updated on 2021-02-09

Interval Operator

Can be imported from here:

1
2
3
import { interval } from 'rxjs';
// or
const { interval } = require('rxjs');

For official documentation, visit https://rxjs-dev.firebaseapp.com/api/index/function/interval

Creates an observable and emits in sequence at specified interval of time.

1
2
3
4
5
6
const { interval } = require('rxjs');
const interval$ = interval(1000);

interval$.subscribe((value) => {
    console.log(value);
})

Output:

1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
# and so on...
1
2
3
4
5
6
7
8
const { interval } = require('rxjs');
const { take } = require('rxjs/operators');

const interval$ = interval(1000).pipe(take(3));

interval$.subscribe((value) => {
    console.log(value);
})

Output:

1
2
3
0
1
2

Emits only 3 values in sequence at time intervals set.

Комментарии