How to checkif there are pending requests (Ajax and its variants) from browser

前端 未结 4 2003
感动是毒
感动是毒 2020-12-28 16:20

Some of the sites I deal with have heavy ajax requests. I plan to wait for Ajax request completion before clicking for asserting for element. Currently I use



        
4条回答
  •  温柔的废话
    2020-12-28 17:07

    Based on our discussion over the comments, this might work for you.

    With prototype.js:

    var ACTIVE_REQUESTS = 0; // GLOBAL
    
    ACTIVE_REQUESTS++
    new Ajax.Request('/your/url', {
      onSuccess: function(response) {
        ACTIVE_REQUESTS--;
        // Handle the response content...
      }
    }));
    
    console.log("there are " + ACTIVE_REQUESTS + " open AJAX requests pending");
    

    With plain script:

    interValRef = 0;
    
    interValRef = setInterval("checkState();",100)
    
    function checkState(){
        if(document.readyState == 'complete'){
            clearInterval(interValRef);
            myFunc();
        }
    }
    

    Source: Check Pending AJAX requests or HTTP GET/POST request

提交回复
热议问题