用Ajax调用顺序运行两个函数
Run two functions with Ajax calls sequentially
我有两个函数里面有ajax calls
。
function a(){
ajaxCall_A();
}
function b(){
ajaxCall_B();
}
我有另一个函数它调用这两个函数按照a
和b
的顺序。
function c(){
a();
b();
// expectation : function 'b' will get executed only after function 'a' finished it's execution
}
我不确定他们是否像我期望的那样工作。有时它可以,但有时却不行。我认为这是因为ajax调用,这是异步的,在他们内部。
我如何运行函数'c'中的两个函数来实现期望。
注意:函数与
下面的函数类似function a(){
$.ajax({
url: "url_a",
type: "post",
dataType: "html",
cache: false,
success: function(data){
},
error:function(){
}
});
}
function b(){
$.ajax({
url: "url_b",
type: "post",
dataType: "html",
cache: false,
success: function(data){
},
error:function(){
}
});
}
因为ajax调用是异步的,所以如果您希望第二个ajax调用在第一个调用完成后才启动,则需要手动对ajax调用进行排序。
promise是唯一适合于序列化异步操作的,它们可以使它变得非常简单。幸运的是,jQuery内置了承诺,并且每个Ajax操作都已经返回了一个承诺,可以用于此:
$.ajax(...).then(function(result1) {
// make 2nd ajax call when the first has completed
return $.ajax(...);
}).then(function(result2) {
// success of both ajax calls here
}, function(err) {
// error here
});
或者,如果您让a()
和b()
都从ajax调用返回jQuery ajax承诺,那么您可以这样做:
a().then(b);
c()
可以是:
function c() {
return a().then(b);
}
所以,如果你想让一个函数调用包含这两个Ajax调用没有a()
和b()
,你应该让它返回一个承诺:
function c() {
return $.ajax(...).then(function(result1) {
// make 2nd ajax call when the first has completed
return $.ajax(...);
})
}
然后,你可以这样调用c()
:
c().then(function(result) {
// success here
}, function(err) {
// error here
});
下面是一个返回Ajax承诺的函数示例:
function test() {
return $.ajax("http://www.test.com/api/test");
}
现在,您已经添加了实际的ajax代码,您可以添加返回:
function a(){
return $.ajax({
url: "url_a",
type: "post",
dataType: "html",
cache: false
});
}
function b(){
return $.ajax({
url: "url_b",
type: "post",
dataType: "html",
cache: false
});
}
function c() {
return a().then(b);
}
确保ajaxCall_A()
和ajaxCall_B()
返回承诺,并在a()
和b()
上添加return
s
function a(){
return ajaxCall_A();
}
function b(){
return ajaxCall_B();
}
那么,a()
和b()
将按如下顺序执行:
function c() {
a().then(b);
}
你还应该添加一个return
到c()
,这样它返回一个承诺给它的调用者。
function c() {
return a().then(b);
}
直接使用jQuery.when() &done() with Deferred:
function a() {
var deferred = $.Deferred();
$.ajax({
url: "url_a",
type: "post",
dataType: "html",
cache: false,
success: function(data){
deferred.resolve(data);
},
error:function(){
deferred.resolve("Error from a()");
}
});
return deferred.promise();
}
function b() {
var deferred = $.Deferred();
$.ajax({
url: "url_b",
type: "post",
dataType: "html",
cache: false,
success: function(data){
deferred.resolve(data);
},
error:function(){
deferred.resolve("Error from b()");
}
});
return deferred.promise();
}
function c()
{
$.when(
a(),
b()
)
.done(function ( v1, v2 ){
//Do your work - v1 and v2 are resolved value from a() and b()
});
}
允许a
函数在完成时接受回调参数,并发送b
函数作为回调:
function c(){
a(b);
}
function a(callback){
$.ajax({
url: "url_a",
type: "post",
dataType: "html",
cache: false,
success: function(data){
callback() // Note here call the next function
},
error:function(){
}
});
}
您需要使用信号量
同步它们。function c(){
$semaphore=true;
a();
while($semaphore ) {
setTimeout("checksemaphore()", 500);
}
b();
// expectation : function 'b' will get executed only after function 'a' finished it's execution
}
inside the ajax call to a
add a complete:
complete : function(result) {
$waitsemaphore= 0 ; },
error: function(result) {alert("AJAX completed w/error");alert(result);},
相关文章:
- JQuery合并了keyup和focusout两个函数
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 递归|两个函数名
- 同一事物的两个函数,一个崩溃,另一个不崩溃,为什么
- 如何调用两个函数是一种html输入类型
- 广义化对两个性质起反作用的两个函数
- 两个函数之间的角度承诺
- 在keyup更改上运行两个函数
- 为什么这两个函数调用不相等
- 在 javascript 中通过继承调用两个函数
- 删除两个函数使用的全局变量
- 我需要将这两个函数从javascript更改为JQuery,任何人都可以告诉我如何转换
- 我需要在单击时运行两个函数.即使嵌套在一起,我也无法让它们都工作
- iMacros iim播放如何调用两个函数
- 尝试连接Angular.js或JavaScript中的两个函数返回
- 我的if和else-if逻辑中的问题以及两个函数的冲突
- 如何在onchange事件中的两个函数之间进行选择
- javascript中的函数重载使两个函数名称相同,但输入数字不同
- 包括javascript按键中的两个函数
- 在onload上调用两个函数不起作用