timeInterval

function stable

Emits an object containing the current value, and the time that has passed between emitting the current value and the previous value, which is calculated by using the provided scheduler's now() method to retrieve the current time at each emission, then calculating the difference. The scheduler defaults to asyncScheduler, so by default, the interval will be in milliseconds.

timeInterval<T>(scheduler: SchedulerLike = async): OperatorFunction<T, TimeInterval<T>>

Parameters

scheduler

Optional. Default is async.

Scheduler used to get the current time.

Returns

OperatorFunction<T, TimeInterval<T>>: A function that returns an Observable that emits information about value and interval.

Description

Convert an Observable that emits items into one that emits indications of the amount of time elapsed between those emissions.

timeInterval marble diagram

Examples

Emit interval between current value with the last value

const seconds = interval(1000);

seconds.pipe(timeInterval())
.subscribe(
    value => console.log(value),
    err => console.log(err),
);

seconds.pipe(timeout(900))
.subscribe(
    value => console.log(value),
    err => console.log(err),
);

// NOTE: The values will never be this precise,
// intervals created with `interval` or `setInterval`
// are non-deterministic.

// {value: 0, interval: 1000}
// {value: 1, interval: 1000}
// {value: 2, interval: 1000}

© 2015–2021 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/operators/timeInterval