Javascript。当前 AJAX 停止后运行 AJAX 流

标签 javascript ajax jquery

所以我有一个简单的 javascript,当用户单击“更多”时,它会从数据库中加载更多评论。 现在我想扩展这个脚本,以便它在开始让用户查看评论之前首先填充 SQL 数据库。我觉得我走在正确的轨道上,但我无法让它发挥作用。

首先是工作的代码。

$(function() {

$('.load_more').live("click",function() {


    var photoid = document.getElementById('photoid').value;
    var lastid = document.getElementById('lastid').value;


    if(lastid!='end'){

        $.ajax({
        type: "POST",
        url: "/more_comments_ajax.php",
        data: {
            photoid : photoid,
            lastid : lastid
        },
        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

        },
        success: function(html){//html = the server response html code
            $("#more").remove();//Remove the div with id=more 
            $("div#updates").append(html);//Append the html returned by the server .


        }
        });

    }
    return false;
});
});

现在觉得这样扩展应该是可以的

$(function() {
        $.ajax({
        type: "POST",
        url: "/populate_sql.php",

        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
        },

    sucess: $('.load_more').live("click",function() {


    var photoid = document.getElementById('photoid').value;
    var lastid = document.getElementById('lastid').value;


    if(lastid!='end'){

        $.ajax({
        type: "POST",
        url: "/more_comments_ajax.php",
        data: {
            photoid : photoid,
            lastid : lastid
        },
        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

        },
        success: function(html){//html = the server response html code
            $("#more").remove();//Remove the div with id=more 
            $("div#updates").append(html);//Append the html returned by the server .


        }
        });

    }
    return false;
});
});
});

我哪里丢了它?

最佳答案

您可以使用此功能。我以前用过它,效果很好。在收到第一个回调后,它会发送第二个请求。

(function($) 
    {
        var ajaxQueue = $({});
        $.ajaxQueue = function(ajaxOpts) 
        {
            var oldComplete = ajaxOpts.complete;
            ajaxQueue.queue(function(next)
            {
                ajaxOpts.complete = function() 
                {
                    if (oldComplete) oldComplete.apply(this, arguments);
                    next();
                };
                $.ajax(ajaxOpts);
            });
        };
    })(jQuery);

像普通的 ajax 一样使用它。示例:

      $.ajaxQueue({ url: 'x.php', data:{x:x,y:y}, type: 'POST', 
        success: function(respond) 
        {
            .....
        }
        });

因此您可以检查是否有来自第一个 ajax 的回调,然后发送第二个请求。 希望对你有帮助。

关于Javascript。当前 AJAX 停止后运行 AJAX 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12162197/

相关文章:

javascript - 仅在 Firefox 中焦点转到下一个文本框

javascript - 创建、编辑或删除新行后表不更新

php - 当多个用户插入新评论时,在 sql 查询中增加无限滚动分页偏移量

php - Ajax /PHP : Returning values

jquery - 使用 jQuery 触发 Click 事件

javascript - 即使值未更改,jQuery UI slider 'change' 事件也会触发

javascript - polymer 适合纸质对话框显示在抽屉顶部

javascript - Angular 翻译 : Child Module translatePartialLoader urlTemplate overrides the Parent Module translatePartialLoader urlTemplate

javascript - 从外部api获取数据

javascript - 关于新网站 DIV 刷新的警报