debounceTime

signature: debounceTime(dueTime: number, scheduler: Scheduler): Observable

Discard emitted values that take less than the specified time between output


:bulb: This operator is popular in scenarios such as type-ahead where the rate
of user input must be controlled!


Examples

Example 1: Debouncing based on time between input

( jsBin |
jsFiddle )

  1. import { fromEvent } from 'rxjs/observable/fromEvent';
  2. import { timer } from 'rxjs/observable/timer';
  3. import { debounceTime, map } from 'rxjs/operators';
  4. const input = document.getElementById('example');
  5. //for every keyup, map to current input value
  6. const example = fromEvent(input, 'keyup').pipe(map(i => i.currentTarget.value));
  7. //wait .5s between keyups to emit current value
  8. //throw away all other values
  9. const debouncedInput = example.pipe(debounceTime(500));
  10. //log values
  11. const subscribe = debouncedInput.subscribe(val => {
  12. console.log(`Debounced Input: ${val}`);
  13. });

Additional Resources


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