This JQuery.when() method in JQuery gives a way to execute callback functions depending on zero or more Thenable objects, which usually are Deferred objects that represent asynchronous events.
Syntax:
jQuery.when(deferreds)
- deferreds: This parameter specifies zero or more Thenable objects.
Parameters:
Return Value: This method returns a Promise.
There are two examples discussed below:
- Example: In this example, the Deferred() is used to create a new object and after that then() method is called with notify and resolve method.
<!DOCTYPE HTML><html><head></script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><p>JQuery.when() method</p><buttononclick="Geeks();">click here</button><pid="GFG_DOWN"></p><script>var def = $.Deferred();function Geeks() {$.when().then(function(a) {alert("when() method called this alert()." );});}</script></body></html>chevron_rightfilter_noneOutput:
Before clicking on button:

After clicking on button:

- Example: In this example, the Deferred() method is used and the state of Deferred object is checked.
<!DOCTYPE HTML><html><head></script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><p>JQuery.when() method</p><buttononclick="Geeks();">click here</button><pid="GFG_DOWN"></p><script>var def = $.Deferred();function Geeks() {$.when(def).done(function (x) {$('#GFG_DOWN').append('when() method is executed.')});def.resolve();}</script></body></html>chevron_rightfilter_noneOutput:

- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery UI | position() Method
- JQuery | parseHTML() method
- jQuery | css() Method
- jQuery | add() method with Example
- jQuery | not() method with Examples
- jQuery | ajaxStart() Method
- JQuery UI | Progressbar method
- jQuery | get() Method
- jQuery | fadeIn() Method
- jQuery | delegate() Method
- jQuery | outerHeight() Method
- jQuery | event.isDefaultPrevented() Method
- jQuery | outerWidth() Method
- jQuery | before() Method
- jQuery | off() Method
- jQuery | html() Method
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


