bufferTime

Buffers the source Observable values for a specific time period.

bufferTime<T>(bufferTimeSpan: number): OperatorFunction<T, T[]>

Parameters

bufferTimeSpan

The amount of time to fill each buffer array.

Returns

OperatorFunction<T, T[]>: An observable of arrays of buffered values.

Description

Collects values from the past as an array, and emits those arrays periodically in time.

Buffers values from the source for a specific time duration bufferTimeSpan. Unless the optional argument bufferCreationInterval is given, it emits and resets the buffer every bufferTimeSpan milliseconds. If bufferCreationInterval is given, this operator opens the buffer every bufferCreationInterval milliseconds and closes (emits and resets) the buffer every bufferTimeSpan milliseconds. When the optional argument maxBufferSize is specified, the buffer will be closed either after bufferTimeSpan milliseconds or when it contains maxBufferSize elements.

Examples

Every second, emit an array of the recent click events

import { fromEvent } from 'rxjs'; import { bufferTime } from 'rxjs/operators'; const clicks = fromEvent(document, 'click'); const buffered = clicks.pipe(bufferTime(1000)); buffered.subscribe(x => console.log(x));

Every 5 seconds, emit the click events from the next 2 seconds

import { fromEvent } from 'rxjs'; import { bufferTime } from 'rxjs/operators'; const clicks = fromEvent(document, 'click'); const buffered = clicks.pipe(bufferTime(2000, 5000)); buffered.subscribe(x => console.log(x));

Overloads

bufferTime(bufferTimeSpan: number, scheduler?: SchedulerLike): OperatorFunction<T, T[]>

Parameters

bufferTimeSpan

Type: number.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, T[]>

bufferTime(bufferTimeSpan: number, bufferCreationInterval: number | null | undefined, scheduler?: SchedulerLike): OperatorFunction<T, T[]>

Parameters

bufferTimeSpan

Type: number.

bufferCreationInterval

Type: number | null | undefined.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, T[]>

bufferTime(bufferTimeSpan: number, bufferCreationInterval: number | null | undefined, maxBufferSize: number, scheduler?: SchedulerLike): OperatorFunction<T, T[]>

Parameters

bufferTimeSpan

Type: number.

bufferCreationInterval

Type: number | null | undefined.

maxBufferSize

Type: number.

scheduler

Optional. Default is undefined.

Type: SchedulerLike.

Returns

OperatorFunction<T, T[]>

See Also