gpt4 book ai didi

javascript - $.post 在 ajaxstop 函数中连续循环

转载 作者:行者123 更新时间:2023-11-30 08:41:37 24 4
gpt4 key购买 nike

我正在尝试在所有 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});
});
});

最佳答案

当您在文档的 ajaxStop 事件上调用 $.post 时,它将循环。 $.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});
});
});

关于javascript - $.post 在 ajaxstop 函数中连续循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com