0
votes

What I am trying to do here in flex is to perform a search as the user types in the search keywords in a textinput. A query runs in a background, fetches the data from sqlite and displays it perfectly fine. The problem is with a large database/large result set. When I query >10000 rows of data, the delay is quite evident. This is a cause of concern when the user keeps typing in and a query is fired for each keystroke.

Example:- lets say the users wants to search for keyword 'hello'. As the user types in, a keyup event attached to the textinput fires queries in the background. So here, there will be 5 keyUp events fired i.e. 5 queries.

What I am trying to achieve here is a bit of performance and cutting loose the irrelevant searches by trying to add a delay to the keyUp event.

<s:TextInput id="searchBox" keyUp="search()"/>

public function search():void{
    searchForKeyWord(StringUtil.trim(searchBox.text));//fires a SQL query
}

Can someone help me understand as to how would I be able to add a delay here (lets say of 2 seconds) before the query gets fired. After 2 seconds, if the input has changed, then there is a delay of 2 more seconds and so on, until the search keyword remains constant for 2 seconds.

Please let me know if there is a better way to do things (rather than adding timers etc).

1
You have no choice to use a timer here. check the time elapsed after a key up, and if the time > 1000 ms or something, fetch data.Eric

1 Answers

1
votes

You could use a Timer. That would suit your situation here. When the user types, search() is entered. If the timer is not running, start it (with a two second delay). Set a listener on the Timer for the timerComplete event, and do the search query in the handler function.

Each time the search() function is entered, reset the timer to start again. This will prevent multiple queries happening while typing is ongoing.

Plenty of Timer examples out there too.