Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,793
    Thanks
    19
    Thanked 156 Times in 147 Posts

    Execute AJAX onLoad?

    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:
    Code:
    <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...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,793
    Thanks
    19
    Thanked 156 Times in 147 Posts
    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...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •