2012-10-22 2 views
3

긴 폴링 기능을 구현하여 레코드의 타임 스탬프를 확인하는 페이지가있어서 데이터가 편집되면 업데이트됩니다.JS에서 긴 폴링 기능 중지

<script type="text/javascript"> 
var poll_url = 'http://mysite.com/poll/; 
var my_ts = <?php $_SESSION['template_timestamp']; ?>; 
(function poll(){ 
    $.ajax({ 
     url: poll_url, 
     type: 'post', 
     success: function(data){ 
      if ((data.ts > 0) { 
       // check if expired 
       if (data.ts > my_ts) { 
        // it has expired, so reload the page 
        $("#dialog-message").html('Record has been edited.'); 
        // show popup 
        $("#dialog-message").dialog({ 
         modal: true, 
         buttons: { 
          Reload: function() { 
           $(this).dialog("close"); 
           $('#pleaseWait').show(); 
           window.location.href = '/records/overview'; 
          } 
         } 
        }); 
       } 
      } else { 
       // is still null 
       console.log('error'); 
      } 
     }, 
     dataType: "json", 
     complete: poll, timeout: 30000 
    }); 
})(); 
</script> 

내가 가진 문제는 전화했을 때 내가 그만 긴 폴링 기능을 강제하고 싶습니다 JS 아약스 호출을 호출하는 다른 액션 있다는 것입니다.
이렇게 할 방법이 있습니까? 당신의 코드에 그 적용

+0

다음 SO 문제는 제공 할 수 있습니다 ...의 라인을 따라 뭔가를 얻을 수 있습니다 대답은 ... http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery –

+0

@StuartWakefield; 고마워요. 그 발췌 문장을 찾지 못했습니다. 그 의견을 답으로 게시하면 동의 할 것입니다. – Rooneyl

답변

5

다음 SO 질문은 제공 할 수 대답 ... Abort Ajax requests using jQuery

, 당신은

<script type="text/javascript"> 
var poll_url = 'http://mysite.com/poll/'; 
var my_ts = <?php $_SESSION['template_timestamp']; ?>; 
var poll_xhr; 

(function poll(){ 
    poll_xhr = $.ajax({ 
     url: poll_url, 
     type: 'post', 
     success: function(data){ 
      // Code removed 
     }, 
     dataType: "json", 
     complete: poll, 
     timeout: 30000 
    }); 
})(); 

// To kill the AJAX request, put this where it makes sense 
poll_xhr.abort(); 
</script>