By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Multiple XMLHTTP Requests

P: n/a
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.
Jan 9 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
*** 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!

--
anthony at my pet programmer dot com
Jan 9 '08 #2

P: n/a
On Jan 9, 3:49 pm, Anthony Levensalor <killf...@mypetprogrammer.com>
wrote:
*** 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!

--
anthony at my pet programmer dot com
Thanks, but I prefer figuring this out rather than using 3-party file.
Jan 9 '08 #3

P: n/a
*** vunet *** wrote a whole bunch of nifty stuff On 1/9/2008 4:23 PM:
On Jan 9, 3:49 pm, Anthony Levensalor <killf...@mypetprogrammer.com>
wrote:
>*** 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!

--
anthony at my pet programmer dot com

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!

--
anthony at my pet programmer dot com
Jan 9 '08 #4

P: n/a
On Wed, 09 Jan 2008 13:23:09 -0800, vunet wrote:
On Jan 9, 3:49 pm, Anthony Levensalor <killf...@mypetprogrammer.com>
wrote:
>*** 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!

--
anthony at my pet programmer dot com

Thanks, but I prefer figuring this out rather than using 3-party file.
Another option would be to go ahead and kick them all off. If my
understanding is correct, they should queue when started. Increment a
counter when it starts, decrement it when one finishes. When the counter
hits zero jump to your next bit of code.

When I needed multiple bits of data returned, I ended up choosing a
server-side method that dropped one big chunk at me that I could then
split up.

if (http.status == 200) {
var json = eval( "(" + http.responseText + ")" );
plist = json["plist"];
mlist = json["map"];
plist2 = json["ody"];
}

yeah, yeah yea.. eval ... I know. Its my server and my data and I'm
the only one with write access to the DB.
Jan 9 '08 #5

P: n/a

"vunet" <vu******@gmail.comwrote in message
news:b5**********************************@d70g2000 hsb.googlegroups.com...
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.
One kludge I used was launch timer before the request checking status of
global var that is updated when status==200.

The timer calls itself for a determined number of callbacks before crapping
out, otherwise when the global var says Ok, then send the next request..
Jan 9 '08 #6

P: n/a
vunet said the following on 1/9/2008 4:23 PM:
On Jan 9, 3:49 pm, Anthony Levensalor <killf...@mypetprogrammer.com>
wrote:
>*** 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!

--
anthony at my pet programmer dot com

Thanks, but I prefer figuring this out rather than using 3-party file.
View Matt's source and figure out how he did it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 9 '08 #7

P: n/a
vunet wrote:
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jan 10 '08 #8

P: n/a
On Jan 9, 3:49*pm, Anthony Levensalor <killf...@mypetprogrammer.com>
wrote:
*** 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.
Jan 10 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.