Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 1.66 KB

debouncetime.md

File metadata and controls

51 lines (34 loc) · 1.66 KB

debounceTime

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

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


💡 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 )

import { fromEvent } from 'rxjs/observable/fromEvent';
import { timer } from 'rxjs/observable/timer';
import { debounceTime, map } from 'rxjs/operators';

const input = document.getElementById('example');

//for every keyup, map to current input value
const example = fromEvent(input, 'keyup').pipe(map(i => i.currentTarget.value));

//wait .5s between keyups to emit current value
//throw away all other values
const debouncedInput = example.pipe(debounceTime(500));

//log values
const subscribe = debouncedInput.subscribe(val => {
  console.log(`Debounced Input: ${val}`);
});

Additional Resources


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