W3cubDocs

/RxJS

timer

function stable

Creates an Observable that starts emitting after an dueTime and emits ever increasing numbers after each period of time thereafter.

timer(dueTime: number | Date = 0, periodOrScheduler?: number | SchedulerLike, scheduler?: SchedulerLike): Observable<number>

Parameters

dueTime

Optional. Default is 0.

The initial delay time specified as a Date object or as an integer denoting milliseconds to wait before emitting the first value of 0`.

periodOrScheduler

Optional. Default is undefined.

The period of time between emissions of the subsequent numbers.

scheduler

Optional. Default is undefined.

The SchedulerLike to use for scheduling the emission of values, and providing a notion of "time".

Returns

Observable<number>: An Observable that emits a 0 after the dueTime and ever increasing numbers after each period of time thereafter.

Description

Its like interval, but you can specify when should the emissions start.

timer marble diagram

timer returns an Observable that emits an infinite sequence of ascending integers, with a constant interval of time, period of your choosing between those emissions. The first emission happens after the specified dueTime. The initial delay may be a Date. By default, this operator uses the async SchedulerLike to provide a notion of time, but you may pass any SchedulerLike to it. If period is not specified, the output Observable emits only one value, 0. Otherwise, it emits an infinite sequence.

Examples

Emits ascending numbers, one every second (1000ms), starting after 3 seconds

import { timer } from 'rxjs';

const numbers = timer(3000, 1000);
numbers.subscribe(x => console.log(x));

Emits one number after five seconds

import { timer } from 'rxjs';

const numbers = timer(5000);
numbers.subscribe(x => console.log(x));

See Also

© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/timer