Multiple XMLHTTP Requests

V

vunet

I'd like to perform multiple XMLHTTP requests but one after the other,
ie. do next when previous one comes back ok.
Identifier to determine when to start the next one is a global
variable whose value is reset to none ("") with XMLHTTP handler
function. For example:

var someGlobal = "";
for(var i=0; i<10; i++){
if(someGlobal ==""){
someGlobal = i+"";
//do XMLHTTP request where handler function makes someGlobal = ""
at the end
}
}

Obviously, this will not work (or will work once) because someGlobal
will NOT be empty for the second loop since request takes some time to
finish and reset someGlobal.

Please advise as to how to implement it best. Thanks.
 
A

Anthony Levensalor

*** vunet *** wrote a whole bunch of nifty stuff On 1/9/2008 4:23 PM:
*** vunet *** wrote a whole bunch of nifty stuff On 1/9/2008 3:43 PM:
[snip]


Please advise as to how to implement it best. Thanks.
http://ajaxtoolbox.com

Already been done for you, by some of the best coders in the business.

All the best,
~A!

Thanks, but I prefer figuring this out rather than using 3-party file.

Sure thing. I'd use an array to queue the XHRs, and since you usually
can only have two going at a time, I'd wait until one completed, then
kick off the next, etc, etc.

Good luck, let me know how it works out for you, I'm interested in
seeing your solution when you get one, just for discussion and
edification. :)

~A!
 
T

Thomas 'PointedEars' Lahn

vunet said:
I'd like to perform multiple XMLHTTP requests but one after the other,
ie. do next when previous one comes back ok. [...]
Please advise as to how to implement it best. Thanks.

// only for demo
var i = 0;

function makeRequest()
{
var me = arguments.callee;

// add alternatives, feature tests and exception handling here
var req = new XMLHttpRequest();
req.open("GET", "/?" + i++, true);
req.onreadystatechange = function()
{
if (req.readyState == 4 && /^(0|2\d\d)$/.test(req.status))
{
window.setTimeout(me, 1000);
}
};

req.send(null);
}

makeRequest();


PointedEars
 
D

David Mark

*** vunet  ***  wrote a whole bunch of nifty stuff On 1/9/2008 3:43 PM:
[snip]


Please advise as to how to implement it best. Thanks.

http://ajaxtoolbox.com

Already been done for you, by some of the best coders in the business.

It was done by one coder and I would hesitate to call him one of the
best in the business.

IIRC, that code does not do exactly what the OP requested (it makes
requests in parallel, rather than sequentially.) Depending on the
OP's needs, that may be the best solution.

Peter Michaux has posted some proposed code for this in a CWR ticket
that looks to be more robust than this example. Unfortunately, the
Trac server has been down lately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top