Executes a function when two AJAX requests are successful
myFunc
If any of the errors are executed
myFailure
。
$.when($.ajax("/page1.php"), $.ajax("/page2.php"))
.then(myFunc, myFailure);
Jquery.when (deferreds)
Parameter deferreds, one or more delay objects or JS objects, we initially think of it as one or more asynchronous requests.
For example:
[JavaScript]View Plaincopyprint?
- $.when ($.ajax ("page1.php"), $.ajax ("page2.php"))
$.when ($.ajax ("page1.php"), $.ajax ("page2.php"))
the When () function is often used in conjunction with the Done () function, fail () function, then () function:
Done (Function func)-Execute function callback when processing in Deferreds is complete
fail (Function func)-Whena function callback is executed when there is a processing failure in the Deferredsthen
(function func1,function func2)-combines the done and fail functions, when both execute func1 successfully, when there is a failure to execute FUNC2
Example:
[JavaScript]View Plaincopyprint?
- var Whenresult = $.when ($.ajax ("page1.php"), $.ajax ("page2.php"));
- Whenresult.done (function(A1,A2) {
- //function content slightly
- //A1 and A2 parameters are related JQXHR objects of two AJAX requests in the When function
- });
- Whenresult.fail (function() {
- //function content slightly
- })
- Whenresult.then (Successfunc,failurefunc);
var whenresult = $.when ($.ajax ("page1.php"), $.ajax ("page2.php")), Whenresult.done (function (A1,A2) {//function content slightly// The A1 and A2 parameters are the related JQXHR objects of two AJAX requests in the When function}), Whenresult.fail (function () {//function contents}) Whenresult.then (Successfunc, FAILUREFUNC);
$.when (). Then ()