javascript - How to delay the .keyup() handler until the user stops typing?
I’ve got a search field. Right now it searches for every keyup. So if someone types “Windows”, it will make a search with AJAX for every keyup: “W”, “Wi”, “Win”, “Wind”, “Windo”, “Window”, “Windows”.
I want to have a delay, so it only searches when the user stops typing for 200 ms.
There is no option for this in thekeyup
function, and I have triedsetTimeout
, but it didn’t work.
How can I do that?
Answer
Solution:
I use this small function for the same purpose, executing a function after the user has stopped typing for a specified amount of time or in events that fire at a high rate, like
resize
:Answer
Solution:
If you want to search after the type is done use a global variable to hold the timeout returned from your
setTimout
call and cancel it with aclearTimeout
if it hasn't yet happend so that it won't fire the timeout except on the lastkeyup
eventOr with an anonymous function :
Answer
Solution:
Another slight enhancement on CMS's answer. To easily allow for separate delays, you can use the following:
If you want to reuse the same delay, just do
If you want separate delays, you can do
Answer
Solution:
You could also look at underscore.js, which provides utility methods like debounce:
Answer
Solution:
Explanation
Use a variable to store the timeout function. Then use
to clear this variable of any active timeout functions, and then use
to set the active timeout function again. We run
clearTimeout()
first, because if a user is typing "hello", we want our function to run shortly after the user presses the "o" key (and not once for each letter).Working Demo
Super simple approach, designed to run a function after a user has finished typing in a text field...
Answer
Solution:
Based on the answer of CMS, I made this :
Put the code below after include jQuery :
And simply use like this :
Careful : the $(this) variable in the function passed as a parameter does not match input
Answer
Solution:
jQuery:
Answer
Solution:
Delay Multi Function Calls using Labels
This is the solution i work with. It will delay the execution on ANY function you want. It can be the keydown search query, maybe the quick click on previous or next buttons ( that would otherwise send multiple request if quickly clicked continuously , and be not used after all). This uses a global object that stores each execution time, and compares it with the most current request.
So the result is that only that last click / action will actually be called, because those requests are stored in a queue, that after the X milliseconds is called if no other request with the same label exists in the queue!
You can set your own delay time ( its optional, defaults to 500ms). And send your function arguments in a "closure fashion".
For example if you want to call the bellow function:
To prevent multiple send_ajax requests, you delay them using:
delay_method( "check date", function(){ send_ajax(2); } ,600);
Every request that uses the label "check date" will only be triggered if no other request is made in the 600 miliseconds timeframe. This argument is optional
Label independency (calling the same target function) but run both:
Results in calling the same function but delay them independently because of their labels being different
Answer
Solution:
If someone like to delay the same function, and without external variable he can use the next script:
Answer
Solution:
This function extends the function from Gaten's answer a bit in order to get the element back:
http://jsfiddle.net/Us9bu/2/
Answer
Solution:
I'm surprised that nobody mention the problem with multiple input in CMS's very nice snipped.
Basically, you would have to define delay variable individually for each input. Otherwise if sb put text to first input and quickly jump to other input and start typing, callback for the first one WON'T be called!
See the code below I came with based on other answers:
This solution keeps setTimeout reference within input's delayTimer variable. It also passes reference of element to callback as fazzyx suggested.
Tested in IE6, 8(comp - 7), 8 and Opera 12.11.
Answer
Solution:
This worked for me where I delay the search logic operation and make a check if the value is same as entered in text field. If value is same then I go ahead and perform the operation for the data related to search value.
Answer
Solution:
Delay function to call up on every keyup. jQuery 1.7.1 or up required
Usage:
$('#searchBox').keyupDelay( cb );
Answer
Solution:
From ES6, one can use arrow function syntax as well.
In this example, the code delays
keyup
event for 400ms after users finish typeing before callingsearchFunc
make a query request.Updated Typescript version:
Answer
Solution:
This is a solution along the lines of CMS's, but solves a few key issues for me:
Usage:
The code is written in a style I enjoy, you may need to add a bunch of semicolons.
Things to keep in mind:
this
works as expected (like in the example).setTimeout
.The solution I use adds another level of complexity, allowing you to cancel execution, for example, but this is a good base to build on.
Answer
Solution:
Combining CMS answer with Miguel's one yields a robust solution allowing concurrent delays.
When you need to delay different actions independently, use the third argument.
Answer
Solution:
Building upon CMS's answer here's new delay method which preserves 'this' in its usage:
Usage:
Answer
Solution:
If you want to do something after a period of time and reset that timer after a specific event like
keyup
, the best solution is made withclearTimeout
andsetTimeout
methods:Answer
Solution:
Use
where expression is the script to run and timeout is the time to wait in milliseconds before it runs - this does NOT hault the script, but simply delays execution of that part until the timeout is done.
will reset/clear the timeout so it does not run the script in expression (like a cancel) as long as it has not yet been executed.
Answer
Solution:
Based on the answer of CMS, it just ignores the key events that doesn't change value.
Answer
Solution:
User lodash javascript library and use _.debounce function
Answer
Solution:
Use the bindWithDelay jQuery plugin:
Answer
Solution:
Answer
Solution:
Here is a suggestion I have written that takes care of multiple input in your form.
This function gets the Object of the input field, put in your code
This is the actual delayCall function, takes care of multiple input fields
Usage:
Answer
Solution:
Take a look at the autocomplete plugin. I know that it allows you to specify a delay or a minimum number of characters. Even if you don't end up using the plugin, looking through the code will give you some ideas on how to implement it yourself.
Answer
Solution:
Well, i also made a piece of code for limit high frequency ajax request cause by Keyup / Keydown. Check this out:
https://github.com/raincious/jQueue
Do your query like this:
And bind event like this:
Answer
Solution:
Saw this today a little late but just want to put this here in case someone else needed. just separate the function to make it reusable. the code below will wait 1/2 second after typing stop.
Answer
Solution:
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: to enable extensions, verify that they are enabled in your .ini files
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.
Answer
Solution:
If you want to search after the type is done use a global variable to hold the timeout returned from your
setTimout
call and cancel it with aclearTimeout
if it hasn't yet happend so that it won't fire the timeout except on the lastkeyup
eventOr with an anonymous function :
Answer
Solution:
Another slight enhancement on CMS's answer. To easily allow for separate delays, you can use the following:
If you want to reuse the same delay, just do
If you want separate delays, you can do
Answer
Solution:
You could also look at underscore.js, which provides utility methods like debounce:
Answer
Solution:
Explanation
Use a variable to store the timeout function. Then use
to clear this variable of any active timeout functions, and then use
to set the active timeout function again. We run
clearTimeout()
first, because if a user is typing "hello", we want our function to run shortly after the user presses the "o" key (and not once for each letter).Working Demo
Super simple approach, designed to run a function after a user has finished typing in a text field...
Answer
Solution:
Based on the answer of CMS, I made this :
Put the code below after include jQuery :
And simply use like this :
Careful : the $(this) variable in the function passed as a parameter does not match input
Answer
Solution:
jQuery:
Answer
Solution:
Delay Multi Function Calls using Labels
This is the solution i work with. It will delay the execution on ANY function you want. It can be the keydown search query, maybe the quick click on previous or next buttons ( that would otherwise send multiple request if quickly clicked continuously , and be not used after all). This uses a global object that stores each execution time, and compares it with the most current request.
So the result is that only that last click / action will actually be called, because those requests are stored in a queue, that after the X milliseconds is called if no other request with the same label exists in the queue!
You can set your own delay time ( its optional, defaults to 500ms). And send your function arguments in a "closure fashion".
For example if you want to call the bellow function:
To prevent multiple send_ajax requests, you delay them using:
delay_method( "check date", function(){ send_ajax(2); } ,600);
Every request that uses the label "check date" will only be triggered if no other request is made in the 600 miliseconds timeframe. This argument is optional
Label independency (calling the same target function) but run both:
Results in calling the same function but delay them independently because of their labels being different
Answer
Solution:
If someone like to delay the same function, and without external variable he can use the next script:
Answer
Solution:
This function extends the function from Gaten's answer a bit in order to get the element back:
http://jsfiddle.net/Us9bu/2/
Answer
Solution:
I'm surprised that nobody mention the problem with multiple input in CMS's very nice snipped.
Basically, you would have to define delay variable individually for each input. Otherwise if sb put text to first input and quickly jump to other input and start typing, callback for the first one WON'T be called!
See the code below I came with based on other answers:
This solution keeps setTimeout reference within input's delayTimer variable. It also passes reference of element to callback as fazzyx suggested.
Tested in IE6, 8(comp - 7), 8 and Opera 12.11.
Answer
Solution:
This worked for me where I delay the search logic operation and make a check if the value is same as entered in text field. If value is same then I go ahead and perform the operation for the data related to search value.
Answer
Solution:
Delay function to call up on every keyup. jQuery 1.7.1 or up required
Usage:
$('#searchBox').keyupDelay( cb );
Answer
Solution:
From ES6, one can use arrow function syntax as well.
In this example, the code delays
keyup
event for 400ms after users finish typeing before callingsearchFunc
make a query request.Updated Typescript version:
Answer
Solution:
This is a solution along the lines of CMS's, but solves a few key issues for me:
Usage:
The code is written in a style I enjoy, you may need to add a bunch of semicolons.
Things to keep in mind:
this
works as expected (like in the example).setTimeout
.The solution I use adds another level of complexity, allowing you to cancel execution, for example, but this is a good base to build on.
Answer
Solution:
Combining CMS answer with Miguel's one yields a robust solution allowing concurrent delays.
When you need to delay different actions independently, use the third argument.
Answer
Solution:
Building upon CMS's answer here's new delay method which preserves 'this' in its usage:
Usage:
Answer
Solution:
If you want to do something after a period of time and reset that timer after a specific event like
keyup
, the best solution is made withclearTimeout
andsetTimeout
methods:Answer
Solution:
Use
where expression is the script to run and timeout is the time to wait in milliseconds before it runs - this does NOT hault the script, but simply delays execution of that part until the timeout is done.
will reset/clear the timeout so it does not run the script in expression (like a cancel) as long as it has not yet been executed.
Answer
Solution:
Based on the answer of CMS, it just ignores the key events that doesn't change value.
Answer
Solution:
User lodash javascript library and use _.debounce function
Answer
Solution:
Use the bindWithDelay jQuery plugin:
Answer
Solution:
Answer
Solution:
Here is a suggestion I have written that takes care of multiple input in your form.
This function gets the Object of the input field, put in your code
This is the actual delayCall function, takes care of multiple input fields
Usage:
Answer
Solution:
Take a look at the autocomplete plugin. I know that it allows you to specify a delay or a minimum number of characters. Even if you don't end up using the plugin, looking through the code will give you some ideas on how to implement it yourself.
Answer
Solution:
Well, i also made a piece of code for limit high frequency ajax request cause by Keyup / Keydown. Check this out:
https://github.com/raincious/jQueue
Do your query like this:
And bind event like this:
Answer
Solution:
Saw this today a little late but just want to put this here in case someone else needed. just separate the function to make it reusable. the code below will wait 1/2 second after typing stop.
Answer
Solution:
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: to enable extensions, verify that they are enabled in your .ini files
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Answer
Solution:
Based on the answer of CMS, I made this :
Put the code below after include jQuery :
And simply use like this :
Careful : the $(this) variable in the function passed as a parameter does not match input
Answer
Solution:
jQuery:
Answer
Solution:
Delay Multi Function Calls using Labels
This is the solution i work with. It will delay the execution on ANY function you want. It can be the keydown search query, maybe the quick click on previous or next buttons ( that would otherwise send multiple request if quickly clicked continuously , and be not used after all). This uses a global object that stores each execution time, and compares it with the most current request.
So the result is that only that last click / action will actually be called, because those requests are stored in a queue, that after the X milliseconds is called if no other request with the same label exists in the queue!
You can set your own delay time ( its optional, defaults to 500ms). And send your function arguments in a "closure fashion".
For example if you want to call the bellow function:
To prevent multiple send_ajax requests, you delay them using:
delay_method( "check date", function(){ send_ajax(2); } ,600);
Every request that uses the label "check date" will only be triggered if no other request is made in the 600 miliseconds timeframe. This argument is optional
Label independency (calling the same target function) but run both:
Results in calling the same function but delay them independently because of their labels being different
Answer
Solution:
If someone like to delay the same function, and without external variable he can use the next script:
Answer
Solution:
This function extends the function from Gaten's answer a bit in order to get the element back:
http://jsfiddle.net/Us9bu/2/
Answer
Solution:
I'm surprised that nobody mention the problem with multiple input in CMS's very nice snipped.
Basically, you would have to define delay variable individually for each input. Otherwise if sb put text to first input and quickly jump to other input and start typing, callback for the first one WON'T be called!
See the code below I came with based on other answers:
This solution keeps setTimeout reference within input's delayTimer variable. It also passes reference of element to callback as fazzyx suggested.
Tested in IE6, 8(comp - 7), 8 and Opera 12.11.
Answer
Solution:
This worked for me where I delay the search logic operation and make a check if the value is same as entered in text field. If value is same then I go ahead and perform the operation for the data related to search value.
Answer
Solution:
Delay function to call up on every keyup. jQuery 1.7.1 or up required
Usage:
$('#searchBox').keyupDelay( cb );
Answer
Solution:
From ES6, one can use arrow function syntax as well.
In this example, the code delays
keyup
event for 400ms after users finish typeing before callingsearchFunc
make a query request.Updated Typescript version:
Answer
Solution:
This is a solution along the lines of CMS's, but solves a few key issues for me:
Usage:
The code is written in a style I enjoy, you may need to add a bunch of semicolons.
Things to keep in mind:
this
works as expected (like in the example).setTimeout
.The solution I use adds another level of complexity, allowing you to cancel execution, for example, but this is a good base to build on.
Answer
Solution:
Combining CMS answer with Miguel's one yields a robust solution allowing concurrent delays.
When you need to delay different actions independently, use the third argument.
Answer
Solution:
Building upon CMS's answer here's new delay method which preserves 'this' in its usage:
Usage:
Answer
Solution:
If you want to do something after a period of time and reset that timer after a specific event like
keyup
, the best solution is made withclearTimeout
andsetTimeout
methods:Answer
Solution:
Use
where expression is the script to run and timeout is the time to wait in milliseconds before it runs - this does NOT hault the script, but simply delays execution of that part until the timeout is done.
will reset/clear the timeout so it does not run the script in expression (like a cancel) as long as it has not yet been executed.
Answer
Solution:
Based on the answer of CMS, it just ignores the key events that doesn't change value.
Answer
Solution:
User lodash javascript library and use _.debounce function
Answer
Solution:
Use the bindWithDelay jQuery plugin:
Answer
Solution:
Answer
Solution:
Here is a suggestion I have written that takes care of multiple input in your form.
This function gets the Object of the input field, put in your code
This is the actual delayCall function, takes care of multiple input fields
Usage:
Answer
Solution:
Take a look at the autocomplete plugin. I know that it allows you to specify a delay or a minimum number of characters. Even if you don't end up using the plugin, looking through the code will give you some ideas on how to implement it yourself.
Answer
Solution:
Well, i also made a piece of code for limit high frequency ajax request cause by Keyup / Keydown. Check this out:
https://github.com/raincious/jQueue
Do your query like this:
And bind event like this:
Answer
Solution:
Saw this today a little late but just want to put this here in case someone else needed. just separate the function to make it reusable. the code below will wait 1/2 second after typing stop.
Answer
Solution:
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: to enable extensions, verify that they are enabled in your .ini files
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Answer
Solution:
Delay Multi Function Calls using Labels
This is the solution i work with. It will delay the execution on ANY function you want. It can be the keydown search query, maybe the quick click on previous or next buttons ( that would otherwise send multiple request if quickly clicked continuously , and be not used after all). This uses a global object that stores each execution time, and compares it with the most current request.
So the result is that only that last click / action will actually be called, because those requests are stored in a queue, that after the X milliseconds is called if no other request with the same label exists in the queue!
You can set your own delay time ( its optional, defaults to 500ms). And send your function arguments in a "closure fashion".
For example if you want to call the bellow function:
To prevent multiple send_ajax requests, you delay them using:
delay_method( "check date", function(){ send_ajax(2); } ,600);
Every request that uses the label "check date" will only be triggered if no other request is made in the 600 miliseconds timeframe. This argument is optional
Label independency (calling the same target function) but run both:
Results in calling the same function but delay them independently because of their labels being different
Answer
Solution:
If someone like to delay the same function, and without external variable he can use the next script:
Answer
Solution:
This function extends the function from Gaten's answer a bit in order to get the element back:
http://jsfiddle.net/Us9bu/2/
Answer
Solution:
I'm surprised that nobody mention the problem with multiple input in CMS's very nice snipped.
Basically, you would have to define delay variable individually for each input. Otherwise if sb put text to first input and quickly jump to other input and start typing, callback for the first one WON'T be called!
See the code below I came with based on other answers:
This solution keeps setTimeout reference within input's delayTimer variable. It also passes reference of element to callback as fazzyx suggested.
Tested in IE6, 8(comp - 7), 8 and Opera 12.11.
Answer
Solution:
This worked for me where I delay the search logic operation and make a check if the value is same as entered in text field. If value is same then I go ahead and perform the operation for the data related to search value.
Answer
Solution:
Delay function to call up on every keyup. jQuery 1.7.1 or up required
Usage:
$('#searchBox').keyupDelay( cb );
Answer
Solution:
From ES6, one can use arrow function syntax as well.
In this example, the code delays
keyup
event for 400ms after users finish typeing before callingsearchFunc
make a query request.Updated Typescript version:
Answer
Solution:
This is a solution along the lines of CMS's, but solves a few key issues for me:
Usage:
The code is written in a style I enjoy, you may need to add a bunch of semicolons.
Things to keep in mind:
this
works as expected (like in the example).setTimeout
.The solution I use adds another level of complexity, allowing you to cancel execution, for example, but this is a good base to build on.
Answer
Solution:
Combining CMS answer with Miguel's one yields a robust solution allowing concurrent delays.
When you need to delay different actions independently, use the third argument.
Answer
Solution:
Building upon CMS's answer here's new delay method which preserves 'this' in its usage:
Usage:
Answer
Solution:
If you want to do something after a period of time and reset that timer after a specific event like
keyup
, the best solution is made withclearTimeout
andsetTimeout
methods:Answer
Solution:
Use
where expression is the script to run and timeout is the time to wait in milliseconds before it runs - this does NOT hault the script, but simply delays execution of that part until the timeout is done.
will reset/clear the timeout so it does not run the script in expression (like a cancel) as long as it has not yet been executed.
Answer
Solution:
Based on the answer of CMS, it just ignores the key events that doesn't change value.
Answer
Solution:
User lodash javascript library and use _.debounce function
Answer
Solution:
Use the bindWithDelay jQuery plugin:
Answer
Solution:
Answer
Solution:
Here is a suggestion I have written that takes care of multiple input in your form.
This function gets the Object of the input field, put in your code
This is the actual delayCall function, takes care of multiple input fields
Usage:
Answer
Solution:
Take a look at the autocomplete plugin. I know that it allows you to specify a delay or a minimum number of characters. Even if you don't end up using the plugin, looking through the code will give you some ideas on how to implement it yourself.
Answer
Solution:
Well, i also made a piece of code for limit high frequency ajax request cause by Keyup / Keydown. Check this out:
https://github.com/raincious/jQueue
Do your query like this:
And bind event like this:
Answer
Solution:
Saw this today a little late but just want to put this here in case someone else needed. just separate the function to make it reusable. the code below will wait 1/2 second after typing stop.
Answer
Solution: