How to resolve a deferred object when the user clicks a button using jQuery ?
Last Updated :
17 Jan, 2023
A deferred object in jQuery represents an operation that is still in progress and can be resolved at a later time. Deferred objects are often used in jQuery to handle asynchronous operations, such as making AJAX requests or waiting for a DOM element to become available. When a deferred object is resolved, it can trigger a callback function to execute, allowing you to perform some action when the deferred operation is complete.
In this article, we will look at how to resolve a deferred object when the user clicks a button using jQuery. We will first discuss the approach to solving this problem and then provide the syntax and two complete code examples that demonstrate how to do it.
Approach: To resolve a deferred object when the user clicks a button using jQuery, you can use the following approach:
- Create a deferred object using the $.Deferred() function.
- Bind a click event to the button using the .click() function.
- Inside the click event handler, resolve the deferred object using the .resolve() function.
- Bind a done callback function to the deferred object using the .done() function.
- Inside the done callback function, include the code that you want to execute when the deferred object is resolved.
Syntax: The following is the syntax for creating a deferred object and resolving it.
var deferred = $.Deferred();
deferred.resolve();
The following is the syntax for binding a done callback function to the deferred object:
deferred.done(function() {
// Code to execute when deferred object is resolved
});
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<script>
$(document).ready(function () {
// Create deferred object
var deferred = $.Deferred();
// Bind click event to button
$('#button').click(function () {
// resolve deferred object
deferred.resolve();
});
// Bind done callback to deferred object
deferred.done(function () {
// Display message when deferred object
// is resolved
$('#message').text(
'Deferred object resolved'
);
});
});
</script>
<style>
body {
margin: 0;
padding: 0;
display: grid;
place-content: center;
background-color: white;
color: black;
}
::selection {
color: white;
background-color: green;
}
button {
border: 2px dotted white;
margin: 20px;
padding: 10px;
width: fit-content;
font: 20px;
}
</style>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>How to check the lock-state of a
callback list using jQuery?
</p>
<button id="button">Click me</button>
<div id="message"></div>
</body>
</html>
Output:
Explanation: In this example, we have an HTML page with a button and a message element. When the user clicks the button, we want to resolve a deferred object and display a message.
To achieve this, we first include the jQuery library in the HTML page and then use the $(document).ready() function to ensure that the code will execute when the DOM is fully loaded.
Inside the $(document).ready() function, we create a deferred object using the $.Deferred() function. This creates an object that can be resolved at a later time.
Next, we bind a click event to the button using the $('#button').click() function. This function will execute when the user clicks the button. Inside the click event handler, we use the deferred.resolve() function to resolve the deferred object.
Finally, we bind a done callback function to the deferred object using the deferred.done() function. This function will execute when the deferred object is resolved. Inside the done callback function, we use the $('#message').text() function to update the text of the message element with the string "Deferred object resolved".
When the user clicks the button, the click event handler will execute and the deferred object will be resolved. This will trigger the done callback function to execute, causing the message to be displayed.
Similar Reads
How to use hide() method on button click using jQuery ? jQuery has a lot of handy methods to get the work done easily. In this article, we will discuss one of them which is the hide() method. We can use this method for various purposes on our webpage and get an efficient result. The very first step will be creating an HTML file and link the jQuery librar
2 min read
What are deferred and promise object in jQuery ? In this article, we will see the deferred and promise object in jQuery, along with understanding their basic implementation and the differences between them.Deferred objects are a fundamental part of the jQuery library, used for handling asynchronous requests and operations. They provide a way to tr
5 min read
How to get server response from an AJAX request using jQuery ? In this article, we will see how we can use jQuery to get the server response to an AJAX request. The jQuery ajax() method implements the basic Ajax functionality in jQuery. It communicates with the server via asynchronous HTTP requests. Syntax:$.ajax(url);$.ajax(url,[options]);Parameters:url: A URL
4 min read
How to call a function automatically after waiting for some time using jQuery? In order to run a function automatically after waiting for some time, we are using the jQuery delay() method. The .delay() method in jQuery which is used to set a timer to delay the execution of the next item in the queue.Syntax: $(selector).delay(para1, para2); Approach: When the Button is clicked
2 min read
How to submit a form or a part of a form without a page refresh using jQuery? How to prevent an HTML form to submit?When submitting a form, clicking the submit button usually navigates to a new route as specified in the form's action attribute. However, this can be done in the background using event.preventDefault(), which blocks the default event behavior. By attaching an on
2 min read
How to set alert message on button click in jQuery ? Setting an alert message on a button click in jQuery means using jQuery to capture the button's click event and trigger a browser alert box. This alert displays a custom message to the user whenever the button is clicked.jQuery CDN Link: We have linked the jQuery in our file using the CDN link. <
1 min read