Created
February 21, 2022 03:14
-
-
Save pedroCX486/092e2d0a8e34eec2b70d048e08e850e8 to your computer and use it in GitHub Desktop.
Debounce Example in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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