Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 21187  / 7 Years ago, tue, october 3, 2017, 12:00:00

I have text box and assigned to it keyup event a search function but I want it to happen with delay, not on every keypress



here is the html code :



<input type=text [(ngModel)]=searchedKPI (keyup)=searchConfigTree()>


and here is ts code :



list = list.filter(item => item.label.toLocaleLowerCase().includes(this.searchedKPI.toLocaleLowerCase())).slice();


and here is the example I wanted to search text string but the event happens 4 times, I want to this happens once only for text string :



enter



what the solution?


More From » angular

 Answers
38

Welcome to the Observable's world. Just use Observable to get the desired result. Get the reference of your input in the component and use this code. debounceTime will let the event to trigger at least after 1 second from the previous trigger. It will let you not to fire on every keyup when user types fast.



Observable.fromEvent(yourInput, 'keyup').debounceTime(1000).subscribe(value => /* */)


In the subscribe method you can write your logic. The value is the value of the input.


[#56329] Thursday, September 28, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tammyb

Total Points: 278
Total Questions: 101
Total Answers: 103

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;