debounceTime helps delay actions until the user stops typing for a moment. This avoids doing work too often, like searching on every key press.
debounceTime for input throttling in Angular
inputObservable.pipe(debounceTime(milliseconds))
Use debounceTime inside an RxJS pipe to delay events.
The milliseconds value sets how long to wait after the last event.
this.searchControl.valueChanges.pipe(debounceTime(300))
fromEvent(inputElement, 'input').pipe(debounceTime(500))
This Angular component uses a FormControl to track input changes. It waits 400 milliseconds after the user stops typing before updating the displayed search term. This reduces how often the app reacts to input.
import { Component } from '@angular/core'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { debounceTime } from 'rxjs/operators'; @Component({ selector: 'app-search', template: ` <label for="search">Search:</label> <input id="search" type="text" [formControl]="searchControl" aria-label="Search input" /> <p>Search term: {{searchTerm}}</p> `, standalone: true, imports: [ReactiveFormsModule] }) export class SearchComponent { searchControl = new FormControl(''); searchTerm = ''; constructor() { this.searchControl.valueChanges.pipe( debounceTime(400) ).subscribe(value => { this.searchTerm = value; }); } }
debounceTime only emits the last value after the delay.
It helps avoid too many updates or API calls while typing.
Make sure to unsubscribe if you use this in components with lifecycle hooks to avoid memory leaks.
debounceTime delays events until the user stops typing for a set time.
It is useful to reduce rapid input events and improve performance.
Use it inside RxJS pipes on input observables like FormControl valueChanges.