的JavaScript。当前AJAX停止后运行AJAX流程

时间:2012-08-28 15:04:29

标签: javascript ajax jquery

所以我有一个简单的javascript,当用户点击More时,它会从数据库加载更多的评论。 现在我想扩展这个脚本,以便在它开始让用户查看注释之前首先填充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;
});
});
});

我在哪里失去它?

2 个答案:

答案 0 :(得分:1)

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

(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的回调然后发送第二个请求。 希望它可以帮助你。

答案 1 :(得分:1)

感谢您的回答。这不是我需要的,但它给了我一个想法,对我有用的解决方案是2个javascripts一起工作。如果有人需要类似的话,我会把代码留在这里。

<script type="text/javascript">
jQuery(function($){
var pid = '<?php echo $ids['0']; ?>';
$.ajax({
type: "POST",
url: "/prepare_sql.php",
data: "pid="+ pid,
beforeSend:  function() {
        $('div#updates').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

    },
    success: function(html) {
        $("div#updates").replaceWith(html);
    }
});
});
</script>


<script type="text/javascript">
$('.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;
});

</script>