How to use keyup with delay in jQuery ?
Last Updated :
01 Jun, 2023
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. Firstly, a variable keyupTimer with the keyword "let" is declared such that a timeout can be set with this variable and can be uniquely identified later in the program. Next, there is an input element with an id of "geeks" which accepts any text data. Also, there is a division element that indicates when the keyup with delay effect occurs. We attach a keypress() method to this particular input element with the parameter of the method being an anonymous function. Inside the function, the clearTimeout() method is called with the parameter being the variable keyupTimer defined in the first step of the script. This is done to ensure that the variable doesn't contain any pre-existing timeout before assigning a new timeout to it.
Thereafter, a new timeout is created with the setTimeout() method to add the delay after the keypress() function which generates the desired effect i.e. keyup with a delay. The setTimeout() method has an anonymous function as its parameter which changes the opacity of the division element from hidden to visible using the fadeIn() method and after some delay again changes the opacity of the division element, this time from visible to hidden using the fadeOut() method. The speed of the fading effects of fadeIn() and fadeOut() are both specified as "fast".
Example: In this example, the setTimeout() method contains a delay of 800 milliseconds or 0.8 seconds and the delay specified between the fadeIn() and the fadeOut() method is 1000 milliseconds or 1 second.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<!-- Basic inline styling -->
<style>
body {
text-align: center;
}
h1 {
color: green;
font-size: 40px;
}
div,
p {
font-size: 25px;
font-weight: bold;
}
input {
margin: 0 auto;
}
input:focus {
outline: none !important;
border: 3px solid green;
box-shadow: 0 0 10px green;
}
/* Initially, the message to display after
keyup with delay is not visible */
div {
margin-top: 1.5rem;
display: none;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<p>jQuery - Use keyup with delay</p>
<input type="text" id="geeks" />
<!-- Message to display after keyup with delay -->
<div>Keyup with delay of 1 second!</div>
<script type="text/javascript">
let keyupTimer;
$("#geeks").keypress(function () {
clearTimeout(keyupTimer);
keyupTimer = setTimeout(function () {
$("div")
.fadeIn("fast")
.delay(1000)
.fadeOut("fast");
}, 800);
});
</script>
</body>
</html>
Output:

Approach 2: Using the keyup(), fadeIn(), delay() and fadeOut() methods in the jQuery library & clearTimeout() and setTimeout() methods in native JavaScript. This approach is very similar to the previous approach but the key distinction is that a keyup() method is attached to the input element instead of a keypress() method.
Example: In this example we are using the above-explained approach.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<!-- Basic inline styling -->
<style>
body {
text-align: center;
}
h1 {
color: green;
font-size: 40px;
}
div,
p {
font-size: 25px;
font-weight: bold;
}
input {
margin: 0 auto;
}
input:focus {
outline: none !important;
border: 3px solid green;
box-shadow: 0 0 10px green;
}
/* Initially, the message to display after
keyup with delay is not visible */
div {
margin-top: 1.5rem;
display: none;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<p>jQuery - Use keyup with delay</p>
<input type="text" id="geeks" />
<!-- Message to display after keyup with delay -->
<div>Keyup with delay of 1 second!</div>
<script type="text/javascript">
let keyupTimer;
$("#geeks").keyup(function () {
clearTimeout(keyupTimer);
keyupTimer = setTimeout(function () {
$("div")
.fadeIn("fast")
.delay(1000)
.fadeOut("fast");
}, 800);
});
</script>
</body>
</html>
Output:

Similar Reads
How to use hide() method in jQuery ? The hide() method in jQuery is used for hiding the selected web elements. In this article, we will discuss the hide() method in detail. This method is generally used for effects or animation in jQuery. It also allows us to animate the behavior (transition) of hiding any specific element. Syntax:.hid
5 min read
What is the use of delay() method in jQuery ? In this article, we will see how to use the delay() method and why to use it in jQuery. The delay() method is used to set a timer to delay the execution of the next item in the queue. Syntax: $(selector).delay(para1, para2); In the below example, first, we create a div of size 250px X 200px and set
1 min read
How to Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in
2 min read
jQuery UI Sortable delay Option jQuery UI sortable widget delay option is used to set the time in milliseconds to define when the sorting should start. Syntax: $( ".selector" ).sortable({ delay: 150 }); Approach: First, add jQuery UI scripts needed for your project. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/theme
1 min read
jQuery UI Resizable delay Option The jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. The jQuery UI Resizable delay Option is used to add some delay to resize an element. Syntax: $(".selector").resizable({ delay: nu
1 min read
jQuery UI Selectable delay Option jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery JavaScript Library. jQuery UI is great for building UI interfaces for the webpages. It can be used to build highly interactive web applications or can be used to add widgets easily. The jQuery UI Selectable delay
1 min read
How to count words in real time using jQuery ? In this article, we will learn how to count words in real-time using jQuery. We have an input field, and when we type, we get the number of words in the input in real time. Approach: First, we select the input field and store its value in a variable. We call a function called wordCount() when a keyb
2 min read
How to append an element in two seconds using jQuery ? Given an HTML element and the task is append an element in the document after few seconds using fadeIn effect with the help of JQuery. Approach: Select the target element to append the element. Use one of the appendTo() or append() method to append the element. Example 1: In this example, the <di
2 min read
jQuery keyup() Method The jQuery keyup() is an inbuilt method that is used to trigger the keyup event whenever the User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function) Here selector is the selected element. Parameters:
2 min read
How to run the code on keypress event using jQuery ? In this article, we will see how to run the code snippet on the keypress event using jQuery. The keypress event is triggered when the keyboard button is pressed by the user. Syntax: $(selector).keypress() In the below example, we are creating a textarea element and when the user starts pressing a ke
1 min read