title: How to properly send mutliple http requests to different servers at a certain time?
body: <p>My current problem is that I have to send MULTIPLE http requests that are stored individually in an array to MULTIPLE servers at a certain time:</p>
  
  <p><strong>EX:</strong> array[0] = "<a href="http://.../" rel="nofollow">http://.../</a>", array[1] = "<a href="http://.../" rel="nofollow">http://.../</a>"</p>
  
  <p>Executing the code at a certain time is easy with setInterval(checktime), but the issue is a "for" loop used to iterate through the server commands being sent not meshing with the setInterval function causing the "for" loop to run through and only send the http to the <strong>LAST</strong> server. Every server request prior to it is skipped.</p>
  
  <p>Here is my JS code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function sendHttpCmds(stbnum){
   var stb_num = stbnum;
   var date = [];
   var xmlhttp = [];
   
   //loop to iterate through the different stb's(servers)
   for(var i = 0; i &lt; stb_num; i++){
  
    var http = stb_prop.http_list[i];
    var httpLength = http.length;
    var firstTime = 1;
    var lastIPcmd = stb_prop.ip_list[i];
  
    function xmlsend(cmd){
              xmlhttp[i].open("GET",http[cmd],false);
              xmlhttp[i].send(null);
       }
       
  
    setInterval(function(){
     date[i] = new Date();
  
     if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp[i] =new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp[i] =new ActiveXObject("Microsoft.XMLHTTP");
     }
  
     if(firstTime === 1){
      document.body.innerHTML += '&lt;br&gt;' + date[i].getHours() + ':' + date[i].getMinutes();
     }
  
     if(date[i].getHours() === 14 &amp;&amp; date[i].getMinutes() === 20 &amp;&amp; firstTime === 1){
      document.body.innerHTML += '&lt;br&gt;&lt;b&gt;The report log for STB #'+(i+1)+' is being collected. Please wait:&lt;/b&gt;&lt;br&gt;';
      //insert progress bar here
      firstTime = 0;
            
            for(cmd = 0; cmd &lt; httpLength; cmd++){
               (function(cmd){
                 setTimeout(function(){
                  xmlsend(cmd);
  
                  if(cmd === (httpLength-1)){
                      setTimeout(function(){
                          xmlhttp[i].open("GET","http://"+lastIPcmd+":8080/remote/processKey?key=poweroff&amp;hold=keyPress",false);
                          xmlhttp[i].send(null);
                          document.body.innerHTML += '&lt;br&gt;The report log for STB #'+(i+1)+' has been collected! &lt;br&gt;';
                      },180000); //set delay to send power off command at 5 minutes                        
                  }
                 },2000*cmd);//set base delay ~1000-2500 ms
                }(cmd));
            }
        }
    },5000)//check every 5 seconds for the correct time
   }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>If anyone could enlighten me on how to properly do this that would be great! Another method I was thinking of would be firing 1 http request to 1 server, wait for it to finish then fire the next 1 to the next server, but I do not know how to do that either :\</p>