I have a page where a user can enter a value into a field and it performs a search and returns it back to the user.
The problem is, if I type too fast, it is making multiple calls and sometimes skips the last character I typed and used the call before it.
I am looking into a way to throttle this and found debounce
.
I don't feel like I am using this correctly though as it seems to have no impact on what I am doing.
// Component
sub: Subscription;
this.sub = this._empSearchService.employeeSearchData$
.debounceTime(1000)
.subscribe(
results => {
if (results) {
...
}
}
);
...
// On Key Up Event
this._empSearchService.searchMultipleEmployees(data)
.then((data: any) => {
this._empSearchService.updateSearchedEmployees(data);
});
// Service
private employeeSearchSub = new BehaviorSubject<any>(null);
employeeSearchData$ = this.employeeSearchSub.asObservable();
updateSearchedEmployees(obj) {
this.employeeSearchSub.next(obj);
};
Any thoughts on this?
My concern is that when I enter 4 characters in under 1 second, I am seeing 4 network calls. I assume I should only see one?