我试图在所有ajax调用完成后调用$.post
函数。只是在连续循环运行时才会触发调用。
var jQuery_1_11_0 = $.noConflict(true);
jQuery_1_11_0(document).ready(function () {
var domain = '<?php echo $url; ?>';
// AJAX 1
$.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {/* some data*/},
beforeSend: function (data) {/* some code */},
complete: function () {
$.getJSON('lib/get-details.html', function(data) {
// some code here
}
});
// AJAX 2
$.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {/*some code*/},
beforeSend: function (data) {/*some code*/},
complete: function () {
$.getJSON('lib/get-details.html', function(data) {
// some code here
}
});
// After those 2 calls (they are actually 6, shortened the code)
// are complete I want to start this one, which starts but run continuously.
$(document).ajaxStop(function() {
$.post('lib/ajax.html?action=update_date',{domain: domain});
});
});
答案 0 :(得分:4)
当您在文档的ajaxStop事件上调用$ .post时,它将循环。 $ .post将创建一个ajax调用,当它完成时,它将是ajaxStop。 您的循环问题可以使用简单的标志变量来解决。 flag将限制仅创建$ .post一次。 :)
var jQuery_1_11_0 = $.noConflict(true);
jQuery_1_11_0(document).ready(function () {
var domain = '<?php echo $url; ?>';
// AJAX 1
$.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {/* some data*/},
beforeSend: function (data) {/* some code */},
complete: function () {
$.getJSON('lib/get-details.html', function(data) {
// some code here
}
});
// AJAX 2
$.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {/*some code*/},
beforeSend: function (data) {/*some code*/},
complete: function () {
$.getJSON('lib/get-details.html', function(data) {
// some code here
}
});
// After those 2 calls (they are actually 6, shortened the code)
// are complete I want to start this one, which starts but run continuously.
var isDone=false;
$(document).ajaxStop(function() {
if(isDone)return;
isDone=true;
$.post('lib/ajax.html?action=update_date',{domain: domain});
});
});
答案 1 :(得分:1)
如果您只等待第一个和第二个ajax而不是完整回调中的请求,那么请使用$.when()
var jQuery_1_11_0 = $.noConflict(true);
jQuery_1_11_0(document).ready(function () {
var domain = '<?php echo $url; ?>';
// AJAX 1
var a1 = $.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {}, // some data},
beforeSend: function (data) {}, // some code},
complete: function () {
$.getJSON('lib/get-details.html', function (data) {
// some code here
})
});
// AJAX 2
var a2 = $.ajax({
type: 'POST',
url: 'lib/ajax.html',
data: {}, //some code},
beforeSend: function (data) {}, //some code},
complete: function () {
$.getJSON('lib/get-details.html', function (data) {
// some code here
})
});
// After those 2 calls (they are actually 6, shortened the code)
// are complete I want to start this one, which starts but run continuously.
$.when(a1, a2).done(function () {
$.post('lib/ajax.html?action=update_date', {
domain: domain
});
});
});