2013-07-18 2 views
2

나는 두 가지 문제가 있습니다. 아약스 요청을하면 잘 처리되지만 상태는 1 초를 기다리고 있습니다. 그것은 나에게 정말로 높게 보인다. 여기 아약스가 1 초 이상 기다리는 이유는 무엇입니까?

여기 Network tab

내가 사용하고있는 아약스 기능입니다 크롬에서 네트워크 탭의 스크린 샷이다.

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; 

     }); 
    } 
+6

아약스는 자체적으로 "대기"하지 않습니다. 서버가 데이터를 보내면 반응합니다. 진정으로 묻는 질문은 '왜 요청을 처리하기 위해 1 초 이상 걸리는지'입니다. –

+0

서버가 요청을 처리하고 처리하는 데 1 초가 걸립니까? 여러분이보고있는 것은 자바 스크립트 코드 실행의 프로파일 링이 아니라 네트워크 요청/응답입니다. AJAX는 서버가 응답하지 않아 대기 중입니다. – David

+0

이것은 일관된 문제입니까? 연결 속도가 느릴 수도 있습니다. 또한 서버가 무엇이든 할 수 있습니다. –

답변

1

문제의 출처로 생각되는 PHP 파일의 프로필을 작성합니다.

나는 Laravel의 프로파일 링 옵션에 익숙하지 모르지만, 당신이 할 수있는 간단한 일이 이것이다 :

class utility{ 

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

     //Your code is here 

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

} 

행운을 빕니다!

1
  1. 잠시 후 AJAX를 잊어 버리십시오. HTML 양식을 만들고 동작은 /laravel/public/utility/submitsubcomment이고 POST와 양식 필드는 이전과 같습니다. 이것은 AJAX가 잘못이 없음을 보여줍니다.
  2. 이 양식을 사용하면 서버가 오래 걸리는 이유를 알아낼 수 있습니다. 단계들 사이에 시간을 기록하도록 PHP를 가져 오면 시간이 오래 걸리는 곳을 발견 할 수 있습니다. 적절한 환경 변수를 설정하고 명령 행에서 실행하는 하네스를 작성하여이 프로세스의 속도를 높일 수 있습니다.

데이터베이스를 사용하는 경우 쿼리에 적절한 인덱스가 있는지 확인하십시오.

1

문제는 데이터베이스가 매우 느린 것입니다. 코드 자체는 완벽했습니다. localhost를 사용하여 로컬 DB에 연결하고 있는데, 127.0.0.1을 사용하는 것보다 훨씬 느립니다. 일단 DB 호스트를 변경하면 문제가 해결되었습니다!

관련 문제