[英]Series of Ajax request doesn't wait for finish
I have series of ajax calls, I want that when the calls get completed the page reloads , I am doing this 我有一系列的ajax调用,我希望在调用完成后重新加载页面,我正在这样做
function verifyFrnds() {
var boxes = $(".matchFrnds:checked").length;
//alert(boxes);
var call = 1;
$(".matchFrnds").each(function (index) {
if ($(this).is(':checked')) {
call++;
var sendData = $(this).val();
$.post('AJAX PAGE', {
sendData: sendData
}, function (data) {
//window.location.reload();
});
//alert(call);
//alert(call + ' ' + boxes + ' ' + (call >= boxes));
if (call >= boxes) {
// window.location.reload();
alert("I am executed");
}
}
});
}
so If I do alert, then all the ajax calls are exexuted and I am shown an alert message, but If I do window.localtion.reload()
then after few ajax calls the page get reloaded, which should be reloaded after ALL
calls. 因此,如果我发出警报,则所有ajax调用都会被释放,并显示一条警告消息,但是如果我执行
window.localtion.reload()
那么在几次ajax调用之后,页面将被重新加载,应在ALL
调用之后重新加载该页面。 My main purpose is to reload the page when all calls are done.Any idea what I am doing wrong. 我的主要目的是在完成所有调用后重新加载页面。任何想法我做错了。 There is already a question on SO but it didn't help me much.
关于SO的问题已经存在 ,但并没有太大帮助。
Try using $.when: 尝试使用$ .when:
function verifyFrnds() {
var deferreds = []; //using an array to save promise interfaces
$(".matchFrnds").each(function (index) {
if ($(this).is(':checked')) {
var sendData = this.value;
//we push each request in array (returned promise interface)
deferreds.push($.post('AJAX PAGE', {
sendData: sendData
}));
}
});
//as $.when only accept deferred(promise) as arguments, we need to apply $.when to each promise of array
$.when.apply(null, deferreds).done(function () {
alert("I am executed once all requests done (success)");
window.location.reload();
});
}
You could chain your ajax call using $.then if you need to do it sequentially. 您可以使用$链接您的ajax调用,然后如果需要顺序执行。 Like this:
像这样:
function verifyFrnds() {
var d = jQuery.Deferred();
var p=d.promise();
$(".matchFrnds:checked").each(function (index) {//instead of .matchFrnds
if ($(this).is(':checked')) {
var sendData = $(this).val();
p.then($.post('AJAX PAGE', {
sendData: sendData
}, function (data) {
//window.location.reload();
})
);
}
});
p.then(function(){
alert("I am executed");
});
d.resolve();
}
Note that for jQuery prior to 1.8, you have to use .pipe instead of .then. 请注意,对于1.8之前的jQuery,您必须使用.pipe而不是.then。 Also check your code, I think there is a mistake, it should be
$(".matchFrnds:checked").each(
instead of $(".matchFrnds").each(
同时检查您的代码,我认为有一个错误,应该是
$(".matchFrnds:checked").each(
而不是$(".matchFrnds").each(
Just like A. Wolff said, use jQuery.when and jQuery.then . 就像A. Wolff所说的那样,使用jQuery.when和jQuery.then 。
Another alternative is: call a method on ajax finish ( jqXHR method always ) . 另一个选择是:在ajax finish上调用一个方法( 始终使用 jqXHR方法)。 Check if call >= boxes ( or whatever condition you like ) in that method.
检查在该方法中是否调用> =框(或您喜欢的任何条件)。 For me, this is a bit easy to understand as compared to another solution.
对我来说,与其他解决方案相比,这有点容易理解。
function verifyFrnds() {
var boxes = $(".matchFrnds:checked").length;
//alert(boxes);
var call = 1;
$(".matchFrnds").each(function (index) {
if ($(this).is(':checked')) {
//call++; Move it to .always Thanks for pointing out Khanh
var sendData = $(this).val();
$.post('AJAX PAGE', {
sendData: sendData
}, function (data) {
//window.location.reload();
}).always ( function() {
// This method will be called on ajax completion. be it a failure or success.
// see .done ( for success only ) ; .fail ( for failure only )
call++;
if (call >= boxes) {
// window.location.reload();
alert("I am executed");
}
} ;
}
});
}
For more on jqXHR object. 有关jqXHR对象的更多信息。 Read: http://api.jquery.com/jQuery.ajax/#jqXHR
阅读: http : //api.jquery.com/jQuery.ajax/#jqXHR
You should increment call
not after the if, but in the success of your ajax calls. 您不应在if之后添加
call
,而应在ajax调用成功之后增加调用。
I advice you to put the reload call in a function you call with setInterval and not in the success ajax returns. 我建议您将重载调用放在使用setInterval调用的函数中,而不要放在成功的ajax返回中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.