RxJS - Transformation Operator bufferTime



This is similar to bufferCount, so here, it will collect the values from the observable on which it is called and emit the bufferTimeSpan is done. It takes in 1 argument, i.e., bufferTimeSpan.

Syntax

bufferTime(bufferTimeSpan: number): Observable

Parameters

bufferTimeSpan − The time to fill the buffer array.

Return value

An observable will be returned, that will have an array of buffered values.

Example

import { fromEvent } from 'rxjs';
import { bufferTime } from 'rxjs/operators';

let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let buffered_array = btn_clicks.pipe(bufferTime(4000));
buffered_array.subscribe(arr => console.log(arr));

In the example the time used is 4seconds, So, bufferTime() operator will accumulate the clicks and after every 4 seconds will display them as shown below.

Output

bufferTime Operator
Advertisements