Learn RxJS
Search…
timer

signature: timer(initialDelay: number | Date, period: number, scheduler: Scheduler): Observable

After given duration, emit numbers in sequence every specified duration.

Examples

Example 1: timer emits 1 value then completes
1
// RxJS v6+
2
import { timer } from 'rxjs';
3
4
//emit 0 after 1 second then complete, since no second argument is supplied
5
const source = timer(1000);
6
//output: 0
7
const subscribe = source.subscribe(val => console.log(val));
Copied!
Example 2: timer emits after 1 second, then every 2 seconds
1
// RxJS v6+
2
import { timer } from 'rxjs';
3
4
/*
5
timer takes a second argument, how often to emit subsequent values
6
in this case we will emit first value after 1 second and subsequent
7
values every 2 seconds after
8
*/
9
const source = timer(1000, 2000);
10
//output: 0,1,2,3,4,5......
11
const subscribe = source.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 1yr ago