I have an input search element that I detect keyup and I want to use debounce to limit request made to the API but I can't get it working. I'm just trying to test debounceTime and distinctUntilChanged.
I have already tried (Keyup) but can't get it working.
<input (keyup)="onKeyUp($event)" id="testInput" autocomplete="off" type="text" name="searchFilterText" class="m-list-search__form-input" value="" placeholder="Search...">
Here is the code from the typescript file.
searchInput = document.getElementById('testInput');
observable: any;
/// <summary>
/// Process the search term provided on key up for the input field.
/// </summary>
/// <param name="searchTerm">The search term.</param>
onKeyUp(event: KeyboardEvent) {
//console.log(event);
let element = event.target as HTMLInputElement;
let value = element.value;
this.observable = fromEvent(this.searchInput, event.type)
.debounceTime(500) // millisecs until value gets emitted.
.distinctUntilChanged()
.subscribe(function (event) {
console.log(event.target.value);
});
}
The expected result is a delayed search result value in the console log using debounceTime and distinctUntilChanged.