jQuery jquery在第一个ajax函数调用完全完成后继续另一个ajax函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12516912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery continue another ajax function after the first ajax function call is fully complete
提问by vzhen
I have 2 ajax call in 2 difference functions. I want to use .click to call these 2 functions . The func1 is inserting data in to database, then func2 is to retrieve the data so my question is how to wait until the func1 fully complete then it only execute the func2.
我在 2 个不同的函数中有 2 个 ajax 调用。我想使用 .click 来调用这两个函数。func1 正在向数据库中插入数据,然后 func2 是检索数据,所以我的问题是如何等到 func1 完全完成然后它才执行 func2。
I tried .delay(), it works but i think this is a stupid solution.
我试过 .delay(),它有效,但我认为这是一个愚蠢的解决方案。
$("#updateLocation").click(function(e){
e.preventdefault;
func1();
func2();
});
return false;
});
function func1(){
$.ajax({
url:'',
});
});
function func2(){
$.ajax({
url:'',
});
});
回答by Explosion Pills
Three ways:
三种方式:
Call func2 on success of func1:
在 func1 成功时调用 func2:
function func1() {
$.ajax({ ... }).done(func2);
}
Use Deferred
API to call func2 when funky completes:
Deferred
当 funky 完成时使用API 调用 func2:
e.preventDefault();
$.when(func1).then(func2);
Make func1 synchronous (not recommended):
使 func1 同步(不推荐):
function func1() {
$.ajax({url: '', async: false});
}
回答by Timothy Groote
Since Ajax calls are asynchronous, the application will not 'pause' until an ajax call is complete, and simply start the next call immediately.
由于 Ajax 调用是异步的,应用程序不会在 ajax 调用完成之前“暂停”,而只需立即开始下一个调用。
JQuery offers a handler that is called when the call is successful, and another one if an error occurs during the call.
JQuery 提供了一个在调用成功时调用的处理程序,如果在调用过程中发生错误,则提供另一个处理程序。
$.ajax({
url: 'ajax/test.html',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
},
error :function(jqXHR, textStatus, errorThrown)
{
alert("something went wrong");
}
});
You will want to call your second AJAX function from the success handler.
您需要从成功处理程序调用第二个 AJAX 函数。
回答by Selvakumar Arumugam
Move func2 inside func1->ajax->succes callback
将 func2 移动到 func1->ajax->succes 回调中
$("#updateLocation").click(function(e) {
e.preventdefault;
func1();
//func2();
});
return false;
});
function func1() {
$.ajax({
url: '',
success: function() { /* other stuff */
func2();
}
});
});
function func2() {
$.ajax({
url: '',
});
});
回答by Adil
Call fun2 in success of func1
func1 成功调用 fun2
function func1(){
$.ajax({
url:'',
success:function(){
func2();
}
})
回答by Tool
You can set the option "async" to false (for the first ajax call).
您可以将选项“async”设置为 false(对于第一次 ajax 调用)。
This means that function 2 will be called after function 1 has received a response, i.e. finished it's execution.
这意味着函数 2 将在函数 1 收到响应后被调用,即完成它的执行。
function func1(){
$.ajax({
url:'',
async: false
});
});
回答by superzebb
you could also pass your function as a parameter like this:
您还可以将您的函数作为参数传递,如下所示:
var secondFunc = function (func) {
//Do ajax req or smt
func("answer", "params");
};
var firstFunc = function() {
secondFunc(
function (answerFromFunc2, paramsFromfunc2) {
var a = answerFromFunc2;
var b = paramsFromfunc2;
}
);
//Do smt after the function request to the server is done
var doSmt = "another func or variable";
};
firstFunc();