首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >监控/监听Ajax readyState

监控/监听Ajax readyState
EN

Stack Overflow用户
提问于 2012-06-12 03:29:02
回答 3查看 624关注 0票数 0

请参考下面的代码:

代码语言:javascript
复制
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。

对于ex:

代码语言:javascript
复制
 <input type='button' value='SEND QUOTE' onclick=\"ajax_update(some params); function_that_fires_when_readystate_is_completed();\">

有什么想法吗?

提前谢谢你!

EN

回答 3

Stack Overflow用户

发布于 2012-06-12 03:42:50

从全局上定义这一点

代码语言:javascript
复制
 var requestCounter = 0;

然后

代码语言:javascript
复制
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
     }

希望能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2012-06-12 03:51:21

使用回调。

JS

代码语言:javascript
复制
function ajax_update(callback) {
  var ajaxRequest = createXMLHttpRequestObject();
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      callback();
    }
  };
  xmlhttp.open(...);
  xmlhttp.send(null);
}

HTML

代码语言:javascript
复制
<input onclick="ajax_update(function_that_listens_to_readyState);">
票数 0
EN

Stack Overflow用户

发布于 2012-06-12 03:58:13

代码语言:javascript
复制
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
    }
}

差不多吧。它真的很难看,但是如果不知道你到底想要做什么,我不能给你一个更好的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10986323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档