...

View Full Version : Execute AJAX onLoad?



chump2877
07-28-2006, 06:55 PM
I've got some AJAX that I'm executing in timed increments in the background of a page....the AJAX is started in the BODY tag's onLoad event handler, and then executes in the background every 5 seconds after the page loads, like so:


<body onLoad="setInterval('whosOnline()',5);">

...where whosOnline() is the JS function that communicates with my server side script (in the background)....The server side script basically checks to see who is online and then returns some HTML to my callback function (which then uses innerHTML to display the results)...

I'm wondering if this is a bad design/implementation of AJAX....I've noticed that the AJAX is slowing down the script, and seems to be hogging resources...in fact, the status bar of the browser indicates that the page has never loaded completely (I absolutely need to eliminate this side effect)....and when I go to another URL, sometimes the new URL doesn;t load properly (like the ongoing AJAX in the background of the preceding page wasn;t terminated properly or something, and prevented loading of the new page?)...and sometimes, the browser crashes when open to the page that is using the AJAX...

Anyway, any of your thoughts or ideas are appreciated...

Pyth007
07-28-2006, 07:34 PM
A few things I noticed...
First, you stated you wanted to have the Ajax run every 5 seconds, but your interval is set for 5 miliseconds (which probably accounts for your slow performance). This is also not only hurting the client's performance, but also your server's! Remember that the time parameter in both setInterval and setTimeout methods uses milliseconds as its basis.

Also, I'm not sure, but I think because you are using setInterval instead of setTimeout, the onload never really ends, hense the constant "loading" in the status bar. Perhaps the first time, use setTimeout and then have your whosOnline() function establish the next setTimeout...

chump2877
07-28-2006, 07:54 PM
Thanks! the milliseconds thing threw me off....I changed it to seconds, and it seems to have fixed all of my problems, including the setInterval/setTimeout issue...setInterval seems to work fine now, even though it is part of the onLoad event...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum