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
How to hide/show an image on button click using jQuery ?
In this article, we will see how we can hide or show any particular image in jQuery when a button gets clicked. This is quite easy to do with some lines of jQuery code. Before we jump to the topic, let's know which methods of jQuery will be used for this. So there is a method called show() and anoth
3 min read
How to send row data when clicking button using JavaScript ?
Sending row data when clicking a button using JavaScript refers to capturing data from a specific table row (or similar structure) when a button within that row is clicked. This data can then be processed or sent to a server for further actions, like form submission.Approach:HTML Table Structure: Ea
2 min read
How to create a basic popup button using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a basic popup button using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ
1 min read
How to create clone of any object using jQuery ?
In this article, we will learn to create a clone of an object using jQuery. This can be achieved using the extend() method of jQuery. The extend() method is used to merge the contents of multiple objects into the object passed as the first parameter. This can be used to clone an array as we can pass
3 min read
How to get the ID of the clicked button using JavaScript/jQuery ?
To get the ID of a clicked button using JavaScript/jQuery means identifying which specific button was clicked within a webpage. This is useful when multiple buttons exist, and you need to distinguish between them based on their unique ID for further processing.So, to get the ID of the clicked button
3 min read
How to get the object's name using jQuery ?
In this article, we will learn how to find the name of an element using jQuery. The name attribute can be applied to multiple elements in HTML and is used to specify a name for any element. The name attribute of any element can be found out using the attr() method. This method is used to find the va
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 set timeout for ajax by using jQuery?
In web programming, the Ajax is used so that the resultant data is shown in the one part of the web page, without reloading the page. The user needs to perform the Ajax request and wants the result within a timeframe. In this scenario, the jquery timeout feature is used in the code. Session timeout
4 min read