监视/侦听 Ajax readyState

Monitor / listen for Ajax readyState

本文关键字:readyState Ajax 侦听 监视      更新时间:2023-09-26

请参阅下面的代码:

function createXMLHttpRequestObject() {
    // will store the reference to the XMLHttpRequest object
    var ajaxRequest;
    // create the XMLHttpRequest object
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // return the created object or display an error message
    if (!ajaxRequest) alert("Error creating the XMLHttpRequest object.");
    else return ajaxRequest;
}

function ajax_update() {
  var ajaxRequest = createXMLHttpRequestObject();
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState != 4){ 
    ...
    }
    if(ajaxRequest.readyState == 4){
    //process JSON data
    }
  }
}

我正在尝试从 ajax_update() 函数外部监视/侦听 ajaxRequest.readyState 值。ajax_update() 在按钮上触发。

我的目标是在函数 ajax_update() 之外,仅在所有 Ajax 调用完成时才触发另一个 JS 函数,这意味着 ajaxRequest.readyState==4。

例如:

 <input type='button' value='SEND QUOTE' onclick='"ajax_update(some params); function_that_fires_when_readystate_is_completed();'">

有什么想法吗?

提前谢谢你!

使用回调。

.JS

function ajax_update(callback) {
  var ajaxRequest = createXMLHttpRequestObject();
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      callback();
    }
  };
  xmlhttp.open(...);
  xmlhttp.send(null);
}

.HTML

<input onclick="ajax_update(function_that_listens_to_readyState);">
ajaxRequest1.onreadystatechange = function(){
    if(ajaxRequest.readyState != 4){ 
    ...
    }
    if(ajaxRequest.readyState == 4){
    //process JSON data
       firstIsReady  = true;
       check();
    }
  }
ajaxRequest2.onreadystatechange = function(){
    if(ajaxRequest.readyState != 4){ 
    ...
    }
    if(ajaxRequest.readyState == 4){
    //process JSON data
       secondIsReady  = true;
       check();
    }
  }
function check() {
    if (firstIsReady && secondIsReady) {
        //both ajax calls completed
    }
}

类似的东西。这真的很丑,但不知道你想做什么,我不能给你更好的方法。

定义此全局性

 var requestCounter = 0;

然后

function ajax_update() {
  var ajaxRequest = createXMLHttpRequestObject();
  //after calling request.open();
    requestCounter++;
  ajaxRequest.onreadystatechange = function(){  
    if(ajaxRequest.readyState != 4){ 
          requestCounter--;
          //error code here
    }
    if(ajaxRequest.readyState == 4){
        //process JSON data
          requestCounter--;
    }
     if (requestCounter == 0)
          onAllRequestComplete();
  }
}

     function onAllRequestComplete(){
        // code that have to run when all requests have been completed
     }

希望有帮助。