jqueryHow do I use the jQuery when function?
The jQuery when()
function is used to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events. It is often used when handling multiple asynchronous operations, such as making multiple Ajax requests.
Example
$.when(
$.ajax("/page1.php"),
$.ajax("/page2.php")
).done(function(page1, page2) {
// Both requests are done
console.log(page1, page2);
});
Output example
<html>...</html> <html>...</html>
The when()
function takes one or more Deferred objects as arguments. When all the Deferred objects are resolved, the done()
callback is executed. The callback is passed the resolved values of all the Deferred objects as arguments.
List of Code Parts
$.when()
- This is the jQuerywhen()
function, which takes one or more Deferred objects as arguments.$.ajax()
- This is the jQueryajax()
function, which is used to make an asynchronous request.done()
- This is thedone()
callback, which is executed when all the Deferred objects are resolved.
Relevant Links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How do I use jQuery to detect window resize events?
- How can I convert jQuery code to vanilla JavaScript?
- How can I use JQuery with Yii2?
- How do I use jQuery UI draggable?
- How do I use jQuery's noconflict mode?
- How do I use jQuery Select2 to select multiple options?
- How do I use jQuery to set styles for an element?
- How do I use jQuery to manipulate an input field?
- How do I use jQuery to add a click event to an element?
See more codes...