Stalled and pending ajax requests by JQuery in Chrome

前端 未结 4 654
生来不讨喜
生来不讨喜 2020-12-08 20:52

Once in a while, my Ajax calls (via JQuery 1.8) in my application are stuck with status \"pending\" for a long time (sometimes up to 17 minutes). I\'ve googled it and all po

4条回答
  •  温柔的废话
    2020-12-08 21:40

    You can try this.

    NOW UPDATED WITH ACTUAL WORKING CODE

    (used some timezone code of my own so excuse the references to timezone)

    First initialize arrays and run your request counting logic

    var request = ( typeof request != 'undefined' && request instanceof Array ) ? request : [];
    var pendingAjax = ( typeof pendingAjax != 'undefined' && pendingAjax instanceof Array ) ? pendingAjax : [];
    

    Use .ajaxStart and .ajaxStop to keep track of the active requests.

    var ajaxActive = 0;
    $( document ).ajaxStart(function() {
      ajaxActive++;
      document.ajaxQueueFull = ajaxActive > 5;
    });
    $(document).ajaxStop(function() {
      ajaxActive--;
      document.ajaxQueueFull = ajaxActive > 5;
    }
    

    Create a function to build new requests

        function ajaxRequestNew(t, u, d, s) {
    
            var instance = request.length + 1;
            request[instance] = $.ajax({
                method: t,
                url: u,
                data: {ajaxPost: d },
                success: s
            });
            return instance;
        }
    

    Now create your request getting the instance number returned

        var instance = ajaxRequestNew('POST', 
                                      'js/ajax_timezone.php', 
                                      { ajaxAction : "ajaxGetTimezone", 
                                        tzoffset : "0", 
                                        tztimezone: "America/New_York" },
                                        function(data) {  }
        );
    

    The $.ajax() function returns a XMLHttpRequest object. So we use a while loop to check when our ajax actually gets sent and aborts other active requests if the request is stalled.

        while(request[instance] === null || (request[instance].readyState < 1 || request[instance].readyState > 4)) {
            if (document.ajaxQueueFull) {
                //abort existing requests
                $.each(request, function(i,v) {
                    if (i !== instance) {
                        request[i].abort();
                    }
                });
            }
        }
    

    Push your request onto the stack

        pendingAjax.push(request[instance]);
    

    Create callbacks for your request

      var timezoneFailCallback = function(data) {
            console.log('fail');
            console.dir(data);
        };
    
        var timezoneSuccessCallback = function(data) {
            console.log('success');
            console.dir(data);
        };
    

    use when to apply the callbacks

        $.when.apply($, pendingAjax).done( timezoneSuccessCallback ).fail( timezoneFailCallback);
    

    The code may need a little tweaking for your app, but hopefully you get the idea. Let me know if you have any questions.

提交回复
热议问题