Emits a notification from the source Observable only after a particular time span has passed without another source emission.
debounceTime is a rate-limiting operator that waits for silence. It’s perfect for search-as-you-type, form validation, and any scenario where you want to wait until the user stops performing an action.
The timeout duration in milliseconds (or the time unit determined internally by the optional scheduler) for the window of time required to wait for emission silence before emitting the most recent source value.
MonoTypeOperatorFunction<T> - A function that returns an Observable that delays the emissions of the source Observable by the specified dueTime, and may drop some values if they occur too frequently.
import { fromEvent, debounceTime } from 'rxjs';const clicks = fromEvent(document, 'click');const result = clicks.pipe(debounceTime(1000));result.subscribe(x => console.log(x));// Only emits when clicking stops for 1 second
import { fromEvent, debounceTime, map } from 'rxjs';// BAD: Creates new subscription on every keystrokeconst search$ = fromEvent(searchInput, 'input').pipe( map(e => (e.target as HTMLInputElement).value));search$.subscribe(term => { // This runs on EVERY keystroke - too many API calls! fetch(`/api/search?q=${term}`);});// GOOD: Debounces before making API callsconst betterSearch$ = fromEvent(searchInput, 'input').pipe( map(e => (e.target as HTMLInputElement).value), debounceTime(300), // Wait for user to stop typing distinctUntilChanged() // Skip if value didn't change);betterSearch$.subscribe(term => { // Only runs after 300ms of silence and value changed fetch(`/api/search?q=${term}`);});