首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >长轴停止函数连续循环中的$.post

长轴停止函数连续循环中的$.post
EN

Stack Overflow用户
提问于 2014-09-15 11:38:47
回答 2查看 967关注 0票数 2

在完成所有ajax调用之后,我正在尝试调用一个$.post函数。调用只是在一个连续循环上运行而被触发。

代码语言:javascript
运行
复制
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});
    });
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-15 11:51:06

当您在文档的$.post事件上调用ajaxStop时,它将循环。$.post将创建一个ajax调用,当它完成时,它将ajaxStop。使用简单的标志变量可以解决循环问题。标志将限制只创建一次$.post。:)

代码语言:javascript
运行
复制
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});
    });
});
票数 6
EN

Stack Overflow用户

发布于 2014-09-15 11:55:08

如果只等待第一和第二个ajax,而不是完整回调中的请求,则使用$.when()

代码语言:javascript
运行
复制
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
        });
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25847222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档