0

请看下面的代码:

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() 在按钮 onlick 上触发。

我的目标是在函数 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();\">

有任何想法吗?

先感谢您!

4

3 回答 3

0
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
    }
}

类似的东西。这真的很难看,但在不知道你想做什么的情况下,我无法给你一个更好的方法来做到这一点。

于 2012-06-11T19:58:13.207 回答
0

全局定义这个

 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
     }

希望有所帮助。

于 2012-06-11T19:42:50.377 回答
0

使用回调。

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);">
于 2012-06-11T19:51:20.557 回答