throttleTime

signature: throttleTime(duration: number, scheduler: Scheduler, config: ThrottleConfig): Observable

Emit first value then ignore for specified duration

Examples

Example 1: Emit first value, ignore for 5s window

( StackBlitz )

// RxJS v6+
import { interval } from 'rxjs';
import { throttleTime } from 'rxjs/operators';

// emit value every 1 second
const source = interval(1000);
/*
  emit the first value, then ignore for 5 seconds. repeat...
*/
const example = source.pipe(throttleTime(5000));
// output: 0...6...12
const subscribe = example.subscribe(val => console.log(val));
Example 2: Emit on trailing edge using config

( StackBlitz )

// RxJS v6+
import { interval, asyncScheduler } from 'rxjs';
import { throttleTime } from 'rxjs/operators';

const source = interval(1000);
/*
  emit the first value, then ignore for 5 seconds. repeat...
*/
const example = source.pipe(throttleTime(
  5000,
  asyncScheduler,
  { trailing: true }
));
// output: 5...11...17
const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttleTime.ts

results matching ""

    No results matching ""