Lets say I have functions like:
可以说我有以下功能:
function A(){
func B();
ajax call last;
}
func B(){
if some condition1
func C();
if some condition2
func D();
if some condition3
func E();
}
func C(){
some ajax cal 1
}
func D(){
ajax call 2
}
func E(){
ajax call 3
}
My problem is ajax call last should be fired only when all the previous ajax calls have completed .i.e ajax call 1 , 2 , 3.
我的问题是ajax调用last只有在所有以前的ajax调用完成后才会被触发.i.ejax调用1,2,3。
2 个解决方案
#1
2
If you're using jQuery then you have access to various calls based on a promise. Calls to $.ajax
return a promise that can be used to defer subsequent calls. See the documentation for the jQuery when
call (docs here).
如果您正在使用jQuery,那么您可以根据承诺访问各种调用。对$ .ajax的调用返回一个可用于推迟后续调用的promise。请在调用时查看jQuery的文档(此处为docs)。
Using when
you can execute a function after a $.ajax
call has completed. You can use the same technique for a single such call to $.ajax
, or multiple calls.
使用何时可以在$ .ajax调用完成后执行函数。对于$ .ajax或多个调用的单个调用,您可以使用相同的技术。
I'm not sure based on your question how you want to chain the calls, but here's an attempt based on your code (untested but this should get you on the right track):
我不确定你的问题是如何根据你的方式来调用,但这是基于你的代码的尝试(未经测试,但这应该让你走上正确的轨道):
func B()
{
var callC = null;
var callD = null;
var callE = null;
if (condition1)
callC = $.ajax("/call/1");
if (condition2)
callD = $.ajax("/call/2");
if (condition3)
callE = $.ajax("/call/3");
$.when(callC, callD, callE).then($.ajax("/call/last"));
}
.then
will execute once the 3 AJAX calls have completed, regardless of their success or failure. Also look at .done
if you only want to continue if all calls return success.
.then将在3个AJAX调用完成后执行,无论其成功与否。如果您只想在所有呼叫都返回成功时继续,请查看.done。
#2
0
Try utilizing return
, .then()
尝试使用return,.then()
function A() {
return B();
}
function B() {
if (some_condition1) {
return C();
};
if (some_condition2) {
return D();
};
if (some_condition3) {
return E();
};
}
function C() {
// some ajax call 1
return $.ajax()
}
function D() {
// ajax call 2
return $.ajax()
}
function E() {
// ajax call 3
return $.ajax()
}
A().then(function() {
// ajax call last;
return $.ajax()
});
#1
2
If you're using jQuery then you have access to various calls based on a promise. Calls to $.ajax
return a promise that can be used to defer subsequent calls. See the documentation for the jQuery when
call (docs here).
如果您正在使用jQuery,那么您可以根据承诺访问各种调用。对$ .ajax的调用返回一个可用于推迟后续调用的promise。请在调用时查看jQuery的文档(此处为docs)。
Using when
you can execute a function after a $.ajax
call has completed. You can use the same technique for a single such call to $.ajax
, or multiple calls.
使用何时可以在$ .ajax调用完成后执行函数。对于$ .ajax或多个调用的单个调用,您可以使用相同的技术。
I'm not sure based on your question how you want to chain the calls, but here's an attempt based on your code (untested but this should get you on the right track):
我不确定你的问题是如何根据你的方式来调用,但这是基于你的代码的尝试(未经测试,但这应该让你走上正确的轨道):
func B()
{
var callC = null;
var callD = null;
var callE = null;
if (condition1)
callC = $.ajax("/call/1");
if (condition2)
callD = $.ajax("/call/2");
if (condition3)
callE = $.ajax("/call/3");
$.when(callC, callD, callE).then($.ajax("/call/last"));
}
.then
will execute once the 3 AJAX calls have completed, regardless of their success or failure. Also look at .done
if you only want to continue if all calls return success.
.then将在3个AJAX调用完成后执行,无论其成功与否。如果您只想在所有呼叫都返回成功时继续,请查看.done。
#2
0
Try utilizing return
, .then()
尝试使用return,.then()
function A() {
return B();
}
function B() {
if (some_condition1) {
return C();
};
if (some_condition2) {
return D();
};
if (some_condition3) {
return E();
};
}
function C() {
// some ajax call 1
return $.ajax()
}
function D() {
// ajax call 2
return $.ajax()
}
function E() {
// ajax call 3
return $.ajax()
}
A().then(function() {
// ajax call last;
return $.ajax()
});