...

View Full Version : AJAX running in timed intervals, help!



chump2877
07-29-2006, 12:08 AM
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:


<?// start the session
session_start();?>

<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>

<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
***********************************************/
</script>

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

<?php //iframe for chat module

include "admin/var.php";

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

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

print "</body>";


?>

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/dynamicindex17/ajaxroutine.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:


<?

// File currentOnline.php

include "connect.php";

$now=date('U');
$deldate=$now-1200;

$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");
while($getmessage3=mysql_fetch_array($getmessage2))
{
print "$getmessage3[sessionname]<br>";
}

?>

RexxCrow
07-29-2006, 04:44 AM
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()

A1ien51
07-29-2006, 04:59 AM
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.

Eric

brothercake
07-31-2006, 04:42 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum