-1

Possible Duplicate:
How do I add a delay in a JavaScript loop?

I want to send some POST request from xmlhttp object using javascript without waiting for the response after some sleep. Inside a for loop, this request is being sent. If i am sending all the request without waiting, my browser becomes not responding for a while also it is not allowed from the server side. How can i put sleep among all these POST request?
This is what i am using-

for (var i = 0; i < users.length; i++) {
    var http = new XMLHttpRequest();
    //set args here, which is based elements of array users
    http.open('POST', '/user/home/index.php', true);
    //Set all headers here then send the request
    http.send(args);
}
Community
  • 1
  • 1
ravi
  • 6,140
  • 18
  • 77
  • 154
  • 1
    Also can you add some code to show us what you are trying to do? – Amitd Oct 14 '12 at 09:29
  • @Amitd: yup. sure... see the edited question. it wasn't possible to put the entire code.. so the main part i have kept here – ravi Oct 14 '12 at 09:39
  • @Amitd — Please don't point people towards W3Schools. That page is particularly bad as it starts out by giving an example that uses a string as the first argument but never explains that syntax or why it shouldn't be used. – Quentin Oct 14 '12 at 09:45

1 Answers1

1

There's no way to delay current script execution. You'll have to use async request and restructure your code.

So if you have your code like this:

function postData() {
    for (var i = 0; i < users.length; i++) {
        var http = new XMLHttpRequest();
        //set args here, which is based elements of array users
        http.open('POST', '/user/home/index.php', true);
        //Set all headers here then send the request
        http.send(args);
        //access request result
        if (http.status == 200) {
            console.log(http.responseText);
        } else {
            console.log('request error');
        }
    }
}

Change it like this:

var userIndex = 0;

function postData() {
    if (userIndex >= users.length) {
        //no more users to process
        return;
    }

    var http = new XMLHttpRequest();
    //set args here, which is based elements of array users
    http.open('POST', '/user/home/index.php', true);

    //set request handler
    http.onreadystatechange = function() {
        if (http.readyState != 4) return;
        if (http.status == 200) {
            console.log(http.responseText);
        } else {
            console.log('request error');
        }
        //process next user index
        userIndex++;
        window.setTimeout(function() {
            postData(); //do it again
        }, 5000); //5 seconds delay
    };

    //Set all headers here then send the request
    http.send(args);
}

postData(); //start the request chain
Jay
  • 4,627
  • 1
  • 21
  • 30
  • Tell me if i am wrong anywhere `Before sending request to the server you are doing state check and logging the response` Right? I am using `async Request` already but my server doesn't allow request in this faster rate. There must be some interval ~m.s. among all the request. – ravi Oct 14 '12 at 10:59
  • @RaviJoshi: Updated code for that case. – Jay Oct 14 '12 at 11:19
  • function `window.setTimeout` needs modification. see [this]( http://stackoverflow.com/a/3583740/1175065) – ravi Oct 14 '12 at 14:25