$.post在ajaxstop函数中不断循环

Adr*_*ian 2 javascript ajax jquery json

我试图$.post在所有 ajax 调用完成后调用一个函数。该调用被触发只是因为它在连续循环中运行。

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});
    });
});
Run Code Online (Sandbox Code Playgroud)

Lax*_*nge 6

当您在文档的 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});
    });
});
Run Code Online (Sandbox Code Playgroud)