Skip to content

Instantly share code, notes, and snippets.

@pedroCX486
Created February 21, 2022 03:14
Show Gist options
  • Save pedroCX486/092e2d0a8e34eec2b70d048e08e850e8 to your computer and use it in GitHub Desktop.
Save pedroCX486/092e2d0a8e34eec2b70d048e08e850e8 to your computer and use it in GitHub Desktop.
Debounce Example in Angular
private subject: Subject<any> = new Subject(); // What we need to trigger the debounce. A subject to keep it simple. Declare as a variable in your component.
this.subject.next(null); // Triggering the debounce using the subject. Call this anywhere. You can also pass anything as value.
this.subject.pipe(debounceTime(5000)).subscribe(() => { // The debounce with a 5 sec delay. Call this in a function onInit.
this.doHttpRequest().subscribe({ // The HttpClient request method to be debounced. Use any you want.
next: data => {
console.log(data);
},
error: error => {
console.log(error);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment