Abort previous ajax request on new request

后端 未结 5 527
感动是毒
感动是毒 2020-11-27 02:57

I have a function that runs an ajax call on the change of an input.

But, there is a chance that the function will be fired again before the previous ajax call has co

5条回答
  •  孤城傲影
    2020-11-27 03:19

     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
        }
    });
    

提交回复
热议问题