简体   繁体   English

使用XMLHttpRequest的open()函数循环访问某些URL

[英]loop through some urls using XMLHttpRequest's open() function

我正在从for循环中进行XMLHttpRequest open()调用。循环遍历一组链接。我想等到readyState变为4(响应到来),然后再进行下一次迭代。我该怎么做?请帮忙

Rather than using a for loop to control the iterations, set up an object that : 与其使用for循环来控制迭代,不如设置一个对象:

1) On creation is passed a 'nextIter' callback object 1)在创建时传递一个'nextIter'回调对象

2) Makes it's own call 2)自己拨打电话

3) Then on readystate4 ... 3)然后在readystate4上...

3a) Does it's own processing 3a)是自己处理吗

3b) Calls the member 'nextIter' function. 3b)调用成员“ nextIter”函数。 This object would be another instance of the same object. 该对象将是同一对象的另一个实例。

This way your for loop would just create an object structure where, 这样,您的for循环只会创建一个对象结构,

objA <whenreadystate4 refs> objB <whenreadystate4 refs> objC ... objA <whenreadystate4引用> objB <whenreadystate4引用> objC ...

... and the calls just chain up as each ajax call finishes, and delegates on to the next in the chain ... that is until the bottom element references null. ...并且每次ajax调用结束时,调用都将链接起来,并委托给链中的下一个...,直到底部元素引用为null为止。

Hopefully that makes sense. 希望这是有道理的。

Btw my existing program looks like this and i want to wait till the readyState becomes 4 before going to the next iteration 顺便说一句,我现有的程序看起来像这样,我想等到readyState变为4之后再进行下一次迭代

onMenuItemCommand: function() {
  var i;//iteration value of the loop

  var xhr=new XMLHttpRequest();//create the XMLHttp request
  var e=content.document.getElementsByTagName('a');// get all the links in the content page

  for( i=0;i<e.length;i++){ // loop goes through the selected links
    webassist.setLink(e[i]);      //set the link element

    if(e[i]!=""){  // check the link's href value is null

      xhr.open('HEAD',e[i] ,true);
      xhr.send(null);

    }
    else{ //if the href value is null set it to color red
      webassist.setColor("#FF0000");//red
    }

    xhr.onreadystatechange=function(evnt){
      if(xhr.readyState==4){
        if(xhr.status==200){
          webassist.setColor("#99FF66");//green
        }
        else{
          webassist.setColor("#FF0000");//red
        }
      }
      else{
        webassist.setColor("#FF00FF"); //pink
      }
    }
  }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM