在完成所有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});
});
});
发布于 2014-09-15 11:51:06
当您在文档的$.post事件上调用ajaxStop时,它将循环。$.post将创建一个ajax调用,当它完成时,它将ajaxStop。使用简单的标志变量可以解决循环问题。标志将限制只创建一次$.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});
});
});
发布于 2014-09-15 11:55:08
如果只等待第一和第二个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
});
});
});
https://stackoverflow.com/questions/25847222
复制相似问题