복붙노트

[JQUERY] 이전 요청에 대한 이전 Ajax 요청을 중단합니다

JQUERY

이전 요청에 대한 이전 Ajax 요청을 중단합니다

해결법


  1. 1.

     var currentRequest = null;    
    
    currentRequest = jQuery.ajax({
        type: 'POST',
        data: 'value=' + text,
        url: 'AJAX_URL',
        beforeSend : function()    {           
            if(currentRequest != null) {
                currentRequest.abort();
            }
        },
        success: function(data) {
            // Success
        },
        error:function(e){
          // Error
        }
    });
    

  2. 2.

    var filterCandidates = function(form){
        //Previous request needs to be aborted.
        var request = $.ajax({
            type: 'POST',
            url: '/echo/json/',
            data: {
                json: JSON.stringify({
                    count: 1
                })
            },
            success: function(data){
                if(typeof data !== 'undefined'){
                    jQuery('.count').text(data.count)
                    console.log(data.count);
                }
            }
        });
        return request;
    };
    
    var ajax = filterCandidates(form);
    

    변수를 저장 한 다음 두 번째 시간을 보내기 전에 필요에 따라 readyState를 확인하고 호출하십시오 ().


  3. 3.수락 된 답변에 대한 변형을하고 질문에 대한 의견에서 채택되었습니다. 이것은 내 응용 프로그램에 훌륭하게 작동했습니다 ....

    수락 된 답변에 대한 변형을하고 질문에 대한 의견에서 채택되었습니다. 이것은 내 응용 프로그램에 훌륭하게 작동했습니다 ....

    jQuery의 $ .post () ....

    var request = null;
    
    function myAjaxFunction(){
         $.ajaxSetup({cache: false}); // assures the cache is empty
         if (request != null) {
            request.abort();
            request = null;
         }
         request = $.post('myAjaxURL', myForm.serialize(), function (data) {
             // do stuff here with the returned data....
             console.log("returned data is ", data);
         });
    }
    

    MyAjaxFunction ()을 원하는만큼 여러 번 호출하고 마지막 하나를 제외한 모든 것을 죽이고 (내 응용 프로그램에 '날짜 선택기'를 사용하고 선택한 날짜에 따라 가격이 변경됩니다. - 위 코드없이 빨리 클릭하면 그들이 올바른 가격을 얻을 수있는 경우 동전 던지기. 그것으로 100 % 올바른!)


  4. 4.이 코드를 사용해보십시오

    이 코드를 사용해보십시오

    var lastCallFired=false;
    
    var filterCandidates = function(form){
    
        if(!lastCallFired){
        var request = $.ajax({
            type: 'POST',
            url: '/echo/json/',
            data: {
                json: JSON.stringify({
                    count: 1
                })
            },
            success: function(data){
                if(typeof data !== 'undefined'){
                    jQuery('.count').text(data.count)
                    console.log(data.count);
                }
            }
        });
            setInterval(checkStatus, 20);
    
        }
    
    };
    
    if(jQuery('#search').length > 0){
        var form = jQuery('#search');
        jQuery(form).find(':input').change(function() {
            filterCandidates(form);
        });
        filterCandidates(form);
    }
    
    var checkStatus = function(){
            if(request && request.readyState != 4){
                request.abort();
            }
        else{
            lastCallFired=true;
        }
    };
    

  5. 5.

    if(typeof window.ajaxRequestSingle !== 'undefined'){
      window.ajaxRequestSingle.abort();
    }
    
    window.ajaxRequestSingle = $.ajax({
      url: url,
      method: 'get',
      dataType: 'json',
      data: { json: 1 },
      success: function (data) {
        //...
      },
      complete: function () {
        delete window.ajaxRequestSingle;
      }
    });
    
  6. from https://stackoverflow.com/questions/19244341/abort-previous-ajax-request-on-new-request by cc-by-sa and MIT license