throttleTime

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

Emit latest value when specified duration has passed.

throttleTime - 图1

Examples

Example 1: Receive latest value every 5 seconds

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { throttleTime } 'rxjs/operators';
  3. //emit value every 1 second
  4. const source = interval(1000);
  5. /*
  6. throttle for five seconds
  7. last value emitted before throttle ends will be emitted from source
  8. */
  9. const example = source.pipe(throttleTime(5000));
  10. //output: 0...6...12
  11. const subscribe = example.subscribe(val => console.log(val));
Example 2: Throttle merged observable

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { merge } from 'rxjs/observable/merge';
  3. import { throttleTime, ignoreElements } from 'rxjs/operators';
  4. const source = merge(
  5. //emit every .75 seconds
  6. interval(750),
  7. //emit every 1 second
  8. interval(1000)
  9. );
  10. //throttle in middle of emitted values
  11. const example = source.pipe(throttleTime(1200));
  12. //output: 0...1...4...4...8...7
  13. 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