chump2877
07-28-2006, 11:08 PM
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>";
}
?>
<?// 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>";
}
?>