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

    AJAX running in timed intervals, help!

    I have an AJAX routine running every 2 seconds in the background of a page, initiated in an onLoad event...here is the relevant code:
    PHP Code:
    <?// start the session

    <script type='text/javascript'>
                function whosOnline()
                    //alert('function accessed');

                    var poststr = '';

                    ajaxpack.postAjaxRequest('currentOnline.php', poststr, processGetPost, 'txt');

                // Define a "callback" function to process the data returned by the Ajax request:

                function processGetPost()
                    var myajax = ajaxpack.ajaxobj;

                    if (myajax.readyState == 4)
                        // if request of file completed

                        //alert('request of file completed');

                        if (myajax.status == 200 || window.location.href.indexOf("http") == -1)
                            // if request was successful or running script locally

                            //alert('request was successful');

                            var data = myajax.responseText;
                            if (data != document.getElementById('whosOn').innerHTML)
                                document.getElementById('whosOn').innerHTML = data;

    <script type="text/javascript" src="ajaxroutine.js">
    * Basic Ajax Routine- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code

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

    <?php //iframe for chat module

    include "admin/var.php";

    "Currently Online:<br><br>";

    "<div id='whosOn'></div>";


    The problem is that the code doesn't actually appear to be executing every 2 seconds after page load....the AJAX runs 2 or 3 times at the beginning of the script (at MOST) and then stops altogether...

    The point of this bit of code is to constantly refresh who is currently online (by accessing a server side script with AJAX to get the database results, and then use innerHTML to update the page content)....

    You'll notice that I plugged in some alert() messages in various place inside my javascript....when uncommented, the alert inside whosOnline() appears forever in 2 sec. increments...so I know that this line of code is working:
    <body onLoad="setInterval('whosOnline()',2000);">
    However, the alerts inside the callback function, processGetPost(), really illustrate the problem...I get the alert messages a couple of times at most after page load, and then there are no more alerts....so I'm assuming that this means that my external script, ajaxroutine.js, is having trouble accessing the XMLHttpRequest object or something...any ideas why?

    The AJAX routine that I'm using can be found here: http://www.dynamicdrive.com/dynamici...jaxroutine.htm

    I'm also wondering if I'm overloading this AJAX routine, so to speak....like it can;t handle requests that occur every 2 seconds? And if so, why?

    Finally, the server side script that the AJAX is accessing doesn't have any errors, but I've included it here for good measure:

    PHP Code:

    // File currentOnline.php

    include "connect.php";


    $delinactive="DELETE from ch_online where time<'$deldate'";
    mysql_query($delinactive) or die(mysql_error());

    $del_leftroom="DELETE from ch_online where time=0";
    mysql_query($del_leftroom) or die(mysql_error());

    $getmessage="Select * from ch_online";
    $getmessage2=mysql_query($getmessage) or die("Could not get messages");

    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!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Thanked 2 Times in 2 Posts
    Could they way those functions are setup be causing the problem? Is there maybe a better way to nest your functions? I was wondering also if using clearIntravel would be needed after each run/loop? I was trying to get those functions to work also and it only caused problems in my JS (for a matrix effect script to reload every 10sec after completing.) When I was using it I noticed it started counting right away at the loading of the page not after the first cycle was finished so maybe that could cause timing issues with your functions? I also could have been using it totally wrong as well.

    ajaxpack.postAjaxRequest('currentOnline.php', poststr, processGetPost, 'txt');

    // Define a "callback" function to process the data returned by the Ajax request:

    function processGetPost()

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Between DC and Baltimore In a Cave
    Thanked 94 Times in 88 Posts
    Put an else statement after the status to see if you are geting sometihng other than 200.

    If you are on a shared/fre server, host might chop this for hitting it too often.

    Personally I would never use setInterva I would use a setTimeout each time I got the request back.

    Download Firefox, Download Firebug, look at the XMLHttpRequest in the log.

    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Senior Coder
    Join Date
    Jun 2002
    near Oswestry
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51
    Personally I would never use setInterva I would use a setTimeout each time I got the request back.
    Exactly - there's no way to know how long a request could take; on a bad day it could be a minute or more. So initiate the request using setTimeout, and only when it completes, initiate the next one.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


    Posting Permissions

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