为什么ajax会等待一秒钟?

时间:2013-07-18 17:50:26

标签: php jquery ajax

我遇到了一个问题。当我发出ajax请求时,它会很好,但是状态等待的时间超过1秒。这对我来说似乎很高。

以下是chrome中网络标签的屏幕截图 Network tab

这是我正在使用的ajax功能。

function subCommentSubmit() {
        $('.subComment').on('submit', function() {

            var url = "/laravel/public/utility/submitsubcomment"; // the script where you handle the form input.
            // Submits the data with ajax, method type is POST
            var currentElement = $(this);
            var thatPar = currentElement.parent().parent();
            var liveSubCommSection = $('> .live-sub-comments', thatPar);
            var commentLoader = $('> .loader-comments > .loader', thatPar);

            var formData = currentElement.serialize();
            $('.new-reply', currentElement).val('').blur().trigger('autosize.resize');

            commentLoader.removeClass('hide').fadeIn(250, function() {
                $.ajax({
                       type: "POST",
                       url: url,
                       data: formData, // serializes the form's elements.
                       success: function(data)
                       {    
                            commentLoader.fadeOut(250, function() {
                                commentLoader.addClass('hide');
                                var response = JSON.parse(data);   
                                var commentPost = $('<li class="single-user-reply"> <div class="user-ava-cont"> <a href="'+ response.userid +'" class="user-ava-a"><img src="../images/avatest1.png"> </a> </div><div class="s-w-user-details"><a href="'+ response.userid +'" class="s-w-poster upop">'+ response.username +' </a> <span class="s-w-timestamp">1 second ago</span><a href="#" class="likes-but notliked active">Like</a> <a href="#" class="likes-but liked">Liked</a><ul class="more-dropdown-cont" role="button"> <li class="dropdown minidrop"><button class="more-dropdown dropdown-toggle" role="button" data-toggle="dropdown"><i class="icon down"></i></button><ul class="dropdown-menu" role="menu" aria-labelledby="people"><li role="presentation"><a class="u-a-a" role="menuitem" tabindex="-1" href="#">Block User</a></li><li role="presentation"><a class="u-a-a" role="menuitem" tabindex="-1" href="#">Report Abuse</a></li></ul></li></ul><div class="s-w-user-post">'+ response.comment +'</div><div class="clear"></div></div></li>');
                                commentPost.hide();
                                liveSubCommSection.append(commentPost.fadeIn(250)); 
                                subCommentSubmit();
                            });
                       }
                }); 
            });
            currentElement.unbind('submit');
            // Ensures it doesn't route the form the normal way, and ajax takes over
            return false;

        });
    }

3 个答案:

答案 0 :(得分:1)

我会分析您的PHP文件,因为这似乎是您问题的根源。

我不熟悉Laravel的分析选项,但你可以做的很简单:

class utility{

    function submitsubcomment(){
        $start = microtime(true);

        //Your code is here

        echo (microtime(true) - $start);
    }

}

祝你好运!

答案 1 :(得分:1)

  1. 暂时忘记AJAX。只需创建一个HTML表单,其操作为/laravel/public/utility/submitsubcomment,方法为POST,表单字段与以前一样。这将证明AJAX没有错。
  2. 使用此表单来确定服务器花费这么长时间的原因。获取PHP以记录步骤之间的时间,然后这将发现事情花费时间的地方。您可以通过创建设置适当环境变量的线束,然后从命令行运行它来加快此过程。
  3. 如果您使用的是数据库,请确保您具有相应的查询索引。

答案 2 :(得分:1)

问题是我的数据库非常慢。代码本身非常好。我使用localhost连接到我的本地数据库,这比使用127.0.0.1慢得多。一旦我更改了数据库主机,就解决了我的问题!