Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay KeyPress function when user is typing, so it doesn't fire a request for each keystroke?

background

I have a number of dropdowns on a page. If you change the first one, the rest of the dropdowns are updated according to what you've selected.

In our case, we deal with fund data. So the first dropdown is "All Fund Types". You select "Hedge Funds", and the next dropdown is filtered by options that only apply to Hedge Funds.

The client is now asking me to put a text field into the mix, which when the user starts typing, will effect those results.

So if they type "USD", the second dropdown will only contain options that have funds with "USD" in the name.

problem

The specific problem that I'm having is that with the code I'm using:

$('#Search').keypress(function () {     // trigger the updating process }); 

It's triggering the search for each key press. So when I type "USD" I'm getting 3 requests immediately - one for "U", one for "US" and one for "USD".

I've tried setting a timeout with this:

$('#Search').keypress(function () {      // I figure 2 seconds is enough to type something meaningful     setTimeout(getFilteredResultCount(), 2000); }); 

but all that does is wait 2 seconds before doing what I've described.

I'm sure this problem has been solved before. Could somebody suggest how to solve this issue?

like image 843
DaveDev Avatar asked May 06 '11 14:05

DaveDev


People also ask

How do you delay the Keyup handler until the user stops typing?

Use a variable to store the timeout function. Then use clearTimeout() to clear this variable of any active timeout functions, and then use setTimeout() to set the active timeout function again.

How do you execute a function only after the user stops typing?

Executing a function after a certain amount of inactivity is known as debouncing. Debouncing can be helpful for many reasons. One of the most popular applications in web development is to execute a search or filter some results after a user has stopped typing text in a textbox.

How do I delay a Keyup event?

In this article, we will see how to use keyup with a delay in jQuery. There are two ways to achieve the same: Approach 1: Using the keypress(), fadeIn(), delay() and fadeOut() methods in the jQuery library and clearTimeout() and setTimeout() methods in native JavaScript.


1 Answers

The way I have done this before is to set a timeout, but clear the existing timeout each time a new key is pressed. That way you should only get the request being sent when the user has stopped typing.

var timeoutId = 0; $('#Search').keypress(function () {      clearTimeout(timeoutId); // doesn't matter if it's 0     timeoutId = setTimeout(getFilteredResultCount, 500);     // Note: when passing a function to setTimeout, just pass the function name.     // If you call the function, like: getFilteredResultCount(), it will execute immediately. }); 

(I'd go for about 500ms timeout.)

like image 125
Tim Rogers Avatar answered Oct 01 '22 21:10

Tim Rogers