By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,838 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,838 IT Pros & Developers. It's quick & easy.

Speed problem!

P: 1
Hi i am in need of help with an ajax related problem on my site. I've coded an active users list which shows the members and guests online. I'm sure you've all seen active user lists before, like the one at the bottom of this forum for example but using ajax i created one which updates without you having to refresh the page. It all works fine except it is causing my site to freeze quite often. I have considered removing it but thn i would have to have a regular boring active users list so i was hoping somebody could help me speed it up and stop the freezing from happening. One thing i'm not sure about is whether it is the ajax or the PHP causing this. Anyway enough talk here's my code:

Expand|Select|Wrap|Line Numbers
  1. function Active (){
  2.  
  3.     self.status = "Sending request"
  4.  
  5.       initialize();
  6.       request.onreadystatechange=function(){
  7.  
  8.             if(request.readyState == 4){
  9.  
  10.                   var active = document.getElementById('active');
  11.                   active.innerHTML = request.responseText 
  12.  
  13.             }
  14.  
  15.       }
  16.  
  17.     var infoStr = "cmd=" + encodeURIComponent(escape('active'));
  18.     var url = "ajaxrequest.php";
  19.  
  20.     request.open('POST',url, true);
  21.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  22.     request.send(infoStr);
  23.  
  24.     var delay = 20000;
  25.     var timerID = setInterval("Active()", delay);
  26.  
  27. }
That function is run on the page load and then every 10 seconds after that.

And here is the PHP:

[PHP]
$time_formatted = date("H:i:s", time());

$result = mysql_query("DELETE FROM active WHERE timeout < '$time_formatted' OR time > '$time_formatted'");
$result = mysql_query("DELETE FROM guests WHERE timeout < '$time_formatted' OR time > '$time_formatted'");

$result = mysql_query("SELECT * FROM active ORDER BY time DESC");

while($row = mysql_fetch_array($result)){

switch($row['membertype']){

default:
$link_class = 'main_class';
$image = '/images/icons/user_online.png';
break;

case"Head Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.png';
break;

case"Head Moderator":
$link_class = 'moderator_class';
$image = '/images/icons/moderator_online.png';
break;

case"Admin":
$link_class = 'admin_class';
$image = '/images/icons/admin_online.png';
break;

case"Moderator":
$link_class = 'moderator_class';
$image = '/images/icons/moderator_online.png';
break;

case"Site tester":
$link_class = 'tester_class';
$image = '/images/icons/tester_online.png';
break;

} ?>

<table cellspacing="0" width="100%">
<tr>
<td width="100%">

<img src="<?php echo $image; ?>">
<span class="<?php echo $link_class ?>">
<a href="http://www.staticfire.co.uk/index.php?page=user_page&user=<?php echo $row['loginname']; ?>">
<?php echo $row['loginname']; ?>
</a>
</span>

</td>
<td width="50%" align="right">

<?php

$time = $row['time'];

$formatted_time = date("H:i",strtotime("$time"));

echo $formatted_time;

?>

</td>
</tr>
</table>

<?php

}

//update active users

if(@$_SESSION['loggedin'] == 'true'){

$loginname = $_SESSION['loginname'];
$membertype = $_SESSION['membertype'];
$ip = $_SERVER["REMOTE_ADDR"];

$result = mysql_query("SELECT * FROM active WHERE loginname='$loginname'");

if (mysql_num_rows($result) < 1) {

$query="INSERT INTO active(
loginname,
time,
timeout,
ip,
membertype
)

VALUES(
'$loginname',
CURTIME(),
ADDTIME(CURTIME(), '00:10:00'),
'$ip',
'$membertype'
)";

$result = mysql_query($query);

}else{

$result = mysql_query("UPDATE active SET time=CURTIME(),timeout=ADDTIME(CURTIME(), '00:10:00') WHERE loginname='$loginname'")
or die(mysql_error());

}


}else{

$ip = $_SERVER["REMOTE_ADDR"];

$result = mysql_query("SELECT * FROM guests WHERE ip='$ip'");

if (mysql_num_rows($result) < 1) {

$query="INSERT INTO guests(
ip,
timeout
)

VALUES(
'$ip',
ADDTIME(CURTIME(), '00:10:00')
)";

$result = mysql_query($query);

}else{

$result = mysql_query("UPDATE guests SET timeout=ADDTIME(CURTIME(), '00:10:00') WHERE ip='$ip'")
or die(mysql_error());

}

}


$result = mysql_query("SELECT * FROM guests");

$guest_no = mysql_num_rows($result);

echo'<small>Guests: '.$guest_no.'</small>';
[/PHP]

If you can see anything in either the ajax or the PHP which may cause this freeze pleae let me know.

-Thanks
Jan 10 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

The setInterval should be outside the function. Can it not be updated every minute instead of every 20 seconds?
Jan 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.