You are reading the article Executing Callback Function Based On Objects updated in October 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Executing Callback Function Based On Objects
Definition of jQuery whenThe jQuery when() function is used to run the callback function based on zero or more Thenable objects or Deferred object. This function is a built-in function in jQuery. The when() function accepts a enable object (an object which exposes a then method and returns a promise). The enable objects are usually Deferred objects which represent asynchronous events.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
jQuery.when(deferreds);Parameters:
deferreds – This is not an optional parameter, that specifies zero or more objects of Thenable objects.
Return value – The return value of this function is a promise (promise is an object with a thenable object that is a then method conforms to the specification).
Working of the jQuery when() functionThe jQuery when() function gives a way to execute one or more objects of the callback function. The object when we pass to the when() function, will return promise objects which conform to the specification with the passed thenable object. Then when the object has been accepted or rejected it will call the respective callback function to execute. If we o not pass any objects to the jQuery when() function, then the function returns an accepted (resolved) promise object’s state.
Examples for jQuery when() functionNext, we write the HTML code to understand this function more clearly with the following example, where the when() function is used to execute the function without passing any object, as below.
Example #1Code:
var def = $.Deferred(); function func() { $.when().then(function(a) { $( “body” ).css( “color”, “red” ).text( “when() method called this text() method.” ); }); }
Output:
As in the above program the Deferred object is created, but it is not passing to the when() function. To the when() function no object is passing as a parameter, so If we do not passes any parameters at all, the jQuery.when() function will return a resolved promise that way we are getting the printed message, which is texting inside the callback function, as we can see in the output.
Example of jQuery when () function for single parameter passing-
Example #2Code:
function func() { $.when( { test1 : 123, test2 : 345 } ).then(function( x ) { var msg = “when() function is called this alert() method. The object passed values are ” +x.test1+ ” and ” +x.test2+ “.”; alert( msg ); }); }
Output:
As in the above program an object is created which contains two properties and is directly passing to the when() function. To the when() function single object is passing as a parameter which is not a Deferred or a Promise, so If we pass non Deferred or a Promise object as parameters, the jQuery.when() function will treat it as a resolved Deferred and callback function attache will be executed. The done Callbacks are passed as above the original parameter, so the fail Callbacks, if we have set, are never called because the Deferred is never rejected.
Example of jQuery when () function for multiple parameter passing.
Example #3Code:
var dobj1 = $.Deferred(); var dobj2 = $.Deferred(); var dobj3 = $.Deferred(); function func() { $.when( dobj1, dobj2, dobj3 ).done(function ( p1, p2, p3 ) { $(‘div’).append(p1) $(‘div’).append(p2) $(‘div’).append(p3) }); dobj1.resolve(); dobj2.resolve( “This is single string value.” ); dobj3.resolve( 11, 21, 34, 42, 55 ); }
Output:
As in the above program, the three objects are created and which are passing to the when() function. To the when() function multiple objects are passing as a parameter which is a Deferred or Promise objects. Later in the event a first Deferred object is resolved to no value, so the respective doneCallback argument is undefined. The second Deferred object is resolved to a single string value, so the respective parameter will be having that value. The third Deferred object is resolved to multiple integer values, so the respective parameter will have an array of those values.
ConclusionThe jQuery when function is a built-in function in jQuery, which is used to execute the callback function based on zero or more Tenable objects.
Recommended ArticlesThis is a guide to jQuery when. Here we also discuss the definition, methods, Working of the jQuery when() function with example. You may also have a look at the following articles to learn more –
You're reading Executing Callback Function Based On Objects
Update the detailed information about Executing Callback Function Based On Objects on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!