0

in a while loop of http requests when sending an HTTP request i want the next request wait a moment to let me get the response from the previous one. Is a setTimeout function will help ?

here is the code :

while(i <= 50){
            http.onreadystatechange = function(){       
                    if(http.readyState == 4){
                        getPosts(http.responseText , i);
                        //alert(http.responseText);
                    }
            }
            http.open("get","../php/php.php?q=getBody&id=comment"+i);       
            http.send();
            //sleep(0); this another function in i send a request to php file to make asleep 
            i++;
        }

Also here is a photo for what happen in firebug: firebug error photo

  • 1
    I'm afraid, you are trying to do wrong thing in the first place. Please, get familiar with callbacks, what are they. – kirilloid Sep 04 '13 at 14:38
  • possible duplicate of [Python while loop conversion to Javascript](http://stackoverflow.com/questions/14327647/python-while-loop-conversion-to-javascript) – Bergi Sep 04 '13 at 16:27

3 Answers3

1

You could delay the execution of the second method via setTimeout, however, that is not the best way to do so in Javascript.

When the response from the server comes, you can execute a callback. That callback should handle your response and send the next request.

It is common to use a library to handle AJAX and i'm sure the one you use has plenty of documentation on the issue of callbacks to get you started. Alternatively, in plain JS, you can look into the onreadystatechange attribute of the XHR object which is your callback.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Tibos
  • 27,507
  • 4
  • 50
  • 64
1

sending an HTTP request i want the next request wait a moment to let me get the response from the previous one.

You're defining the use case for callbacks here.

AJAX functions usually have a success or complete function to which you can bind a function which will fire when the response returns. You could make your ajax calls in your loop as long as one result is not dependent upon a previous result, and the callback would independently handle processing each response so you don't have to wait a bit in your code, it's just handled by the event firing the callback.

http://api.jquery.com/jQuery.ajax/

Take a look at the "complete" parameter for the jquery ajax call.

J E Carter II
  • 1,436
  • 1
  • 22
  • 39
0

JavaScript doesn't have a sleep function and its nature makes sleep functions break things.

You could delay a function running with setTimeout but that (and any mythical sleep function) would require that you know how long it takes to send the request and get the response … and you can't know that because networks aren't that predictable.

If you want to wait until you get a response before sending a new request, then put the code to send the new request in the load / readystatechange handler for the previous one.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335