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

multiple xmlhttprequests getting lost

P: n/a
Hi,

I've got multiple xmlhttprequests which are in a loop. So this is the
loop I have so far, with the closure given to me in a previous post:

//-------------------------------------------------------------------------------------
for (i=0; i<data.length; i++) {
httpRequest[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].onreadystatechange = function(index) {
return function () {
if (httpRequest[index].readyState == 4) {
if (httpRequest[index].status == 200) {
var stats = getStats(httpRequest[index].responseText,
data[index]);
updateStats(statsTable.rows[index], stats);
}
}
};
}(i);
httpRequest[i].open('GET', URL, true);
httpRequest[i].send(null);

}
//-------------------------------------------------------------------------------------

So basically, the getStats method parses the response to get the info
I need for that particular request and then updates the html of the
current page accordingly. This works fine for 1, 2, or 3 data
requests. But if I have more, some of the httprequests get lost and
not all parts of my page are updated. But, if I throw in an alert in
each loop, all page data or at least more of them are updated, once
per time after each loop. It's like the alerts slow the loop down and
allow the requests and updates to catch up to the loop processing.

So, I'm not sure what's happening when the alerts aren't there. Are
some of the requests getting clobbered somehow? Are the httprequests
fine, and maybe the original document can only be updated once at a
time?

Mar 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 6, 7:09 pm, jeff.maild...@gmail.com wrote:
Hi,

I've got multiple xmlhttprequests which are in a loop. So this is the
loop I have so far, with the closure given to me in a previous post:

//-------------------------------------------------------------------------------------
for (i=0; i<data.length; i++) {
httpRequest[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].onreadystatechange = function(index) {
return function () {
if (httpRequest[index].readyState == 4) {
if (httpRequest[index].status == 200) {
var stats = getStats(httpRequest[index].responseText,
data[index]);
updateStats(statsTable.rows[index], stats);
}
}
};
}(i);
httpRequest[i].open('GET', URL, true);
httpRequest[i].send(null);

}

//-------------------------------------------------------------------------------------

So basically, the getStats method parses the response to get the info
I need for that particular request and then updates the html of the
current page accordingly. This works fine for 1, 2, or 3 data
requests. But if I have more, some of the httprequests get lost and
not all parts of my page are updated. But, if I throw in an alert in
each loop, all page data or at least more of them are updated, once
per time after each loop. It's like the alerts slow the loop down and
allow the requests and updates to catch up to the loop processing.

So, I'm not sure what's happening when the alerts aren't there. Are
some of the requests getting clobbered somehow? Are the httprequests
fine, and maybe the original document can only be updated once at a
time?
Actually, the alerts totally stop the loop in its place until you
click ok on the alert box. So you're right, with the alerts in there
you're allowing the request and updates to catch up.

I recommend finding a better way to debug than using alerts, since
they change how your code works. I use an amazing firefox extension,
FireBug, to debug javascript. Instead of alert('request sent'); you
do console.log('request sent');, and it will just print it out to the
firebug console which will be at the bottom of your firefox.

Not exactly sure how to solve your bigger problem, but this should
solve your debugging problem.

good luck
Dan

Mar 7 '07 #2

P: n/a
On Mar 7, 5:24 pm, "Dan Silberman" <silber...@gmail.comwrote:
On Mar 6, 7:09 pm, jeff.maild...@gmail.com wrote:
Hi,
I've got multiple xmlhttprequests which are in a loop. So this is the
loop I have so far, with the closure given to me in a previous post:
//-------------------------------------------------------------------------*------------
for (i=0; i<data.length; i++) {
httpRequest[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].onreadystatechange = function(index) {
return function () {
if (httpRequest[index].readyState == 4) {
if (httpRequest[index].status == 200) {
var stats = getStats(httpRequest[index].responseText,
data[index]);
updateStats(statsTable.rows[index], stats);
}
}
};
}(i);
httpRequest[i].open('GET', URL, true);
httpRequest[i].send(null);
}
//-------------------------------------------------------------------------*------------
So basically, the getStats method parses the response to get the info
I need for that particular request and then updates the html of the
current page accordingly. This works fine for 1, 2, or 3 data
requests. But if I have more, some of the httprequests get lost and
not all parts of my page are updated. But, if I throw in an alert in
each loop, all page data or at least more of them are updated, once
per time after each loop. It's like the alerts slow the loop down and
allow the requests and updates to catch up to the loop processing.
So, I'm not sure what's happening when the alerts aren't there. Are
some of the requests getting clobbered somehow? Are the httprequests
fine, and maybe the original document can only be updated once at a
time?

Actually, the alerts totally stop the loop in its place until you
click ok on the alert box. So you're right, with the alerts in there
you're allowing the request and updates to catch up.

I recommend finding a better way to debug than using alerts, since
they change how your code works. I use an amazing firefox extension,
FireBug, to debug javascript. Instead of alert('request sent'); you
do console.log('request sent');, and it will just print it out to the
firebug console which will be at the bottom of your firefox.

Not exactly sure how to solve your bigger problem, but this should
solve your debugging problem.

good luck
Dan


Yeah, I like that log idea. I've just recently started using venkman
with these scripts, but the way I have it now, it halts execution at
whatever break point I setup and I figured it'd be the same as an
alert. I'll look into firebug. Thanks.

Mar 8 '07 #3

P: n/a
On Mar 7, 9:10 pm, jeff.maild...@gmail.com wrote:
On Mar 7, 5:24 pm, "Dan Silberman" <silber...@gmail.comwrote:
On Mar 6, 7:09 pm, jeff.maild...@gmail.com wrote:
Hi,
I've got multiple xmlhttprequests which are in a loop. So this is the
loop I have so far, with the closure given to me in a previous post:
//-------------------------------------------------------------------------**------------
for (i=0; i<data.length; i++) {
httpRequest[i] = false;
httpRequest[i] = new XMLHttpRequest();
httpRequest[i].onreadystatechange = function(index) {
return function () {
if (httpRequest[index].readyState == 4) {
if (httpRequest[index].status == 200) {
var stats = getStats(httpRequest[index].responseText,
data[index]);
updateStats(statsTable.rows[index], stats);
}
}
};
}(i);
httpRequest[i].open('GET', URL, true);
httpRequest[i].send(null);
}
//-------------------------------------------------------------------------**------------
So basically, the getStats method parses the response to get the info
I need for that particular request and then updates the html of the
current page accordingly. This works fine for 1, 2, or 3 data
requests. But if I have more, some of the httprequests get lost and
not all parts of my page are updated. But, if I throw in an alert in
each loop, all page data or at least more of them are updated, once
per time after each loop. It's like the alerts slow the loop down and
allow the requests and updates to catch up to the loop processing.
So, I'm not sure what's happening when the alerts aren't there. Are
some of the requests getting clobbered somehow? Are the httprequests
fine, and maybe the original document can only be updated once at a
time?
Actually, the alerts totally stop the loop in its place until you
click ok on the alert box. So you're right, with the alerts in there
you're allowing the request and updates to catch up.
I recommend finding a better way to debug than using alerts, since
they change how your code works. I use an amazing firefox extension,
FireBug, to debug javascript. Instead of alert('request sent'); you
do console.log('request sent');, and it will just print it out to the
firebug console which will be at the bottom of your firefox.
Not exactly sure how to solve your bigger problem, but this should
solve your debugging problem.
good luck
Dan

Yeah, I like that log idea. I've just recently started using venkman
with these scripts, but the way I have it now, it halts execution at
whatever break point I setup and I figured it'd be the same as an
alert. I'll look into firebug. Thanks.
Ok so firebug seems to work well, but instead of just console.log() I
have to put
FirebugContext.window.console.log('message').

Mar 8 '07 #4

P: n/a
I've got multiple xmlhttprequests which are in a loop.

Well, that could be the problem: "multiple xmlhttprequests in a loop".
The standard says a client *should not* maintain more than 2
connections with any server (see RFC 2616). By default, Firefox (and
IE, I believe) makes 2 connections, while Opera makes 8. Of course,
this may not be the real problem, it's only an hypothesis. However,
rewrite your code in order to keep track of which requests are sent
and which requests have a response, so you only make a maximum of 2
connection at a time. Example:
Suppose you want to make 5 requests: A, B, C, D, and E. The order
could be:
A -request
B -request
A <- response
C -request
B <- response
D -request
D <- response
E -request
C <- response
E <- response
The order may not be like this one, depending on the time needed for
the server to send a response.

If, however, this is not the issue you're having, you shoudln't try to
make more than 2 requests at a time in any app.

Mar 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.