473,890 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Speed problem!

1 New Member
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("DE LETE FROM active WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");
$result = mysql_query("DE LETE FROM guests WHERE timeout < '$time_formatte d' OR time > '$time_formatte d'");

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

while($row = mysql_fetch_arr ay($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.pn g';
break;

case"Head Moderator":
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;

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

case"Moderator" :
$link_class = 'moderator_clas s';
$image = '/images/icons/moderator_onlin e.png';
break;

case"Site tester":
$link_class = 'tester_class';
$image = '/images/icons/tester_online.p ng';
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",strt otime("$time")) ;

echo $formatted_time ;

?>

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

<?php

}

//update active users

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

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

$result = mysql_query("SE LECT * FROM active WHERE loginname='$log inname'");

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($qu ery);

}else{

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

}


}else{

$ip = $_SERVER["REMOTE_ADD R"];

$result = mysql_query("SE LECT * 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($qu ery);

}else{

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

}

}


$result = mysql_query("SE LECT * FROM guests");

$guest_no = mysql_num_rows( $result);

echo'<small>Gue sts: '.$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
1 1615
acoder
16,027 Recognized Expert Moderator MVP
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

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

Similar topics

34
2501
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args, callable(*args)) return proxy which, is functionally equivalent to
52
3884
by: Neuruss | last post by:
It seems there are quite a few projects aimed to improve Python's speed and, therefore, eliminate its main limitation for mainstream acceptance. I just wonder what do you all think? Will Python (and dynamic languages in general) be someday close to compiled languages speed? What will be the future of Psyco, Pypy, Starkiller, Ironpython and all the other projects currently on development?
4
4364
by: Lost Bits | last post by:
Hello there C++ pros.. Well, I have recently redesigned an already existing piece of code that someone else designed - its just a search algorithm they developed, and has to perfomr millions of loops depending on the data input.. My problem is that my code can be upto 10 times slower than the previous persons - and speed is very critical. (and I dont mean setting up the arrays takes long, I'm more concernted about the speed
60
10184
by: Neil | last post by:
I have a situation with an ODBC linked view in an Access 2000 MDB with a SQL 7 back end. The view is scrolling very slowly. However, if I open the view in an ADP file, it scrolls quickly. I needed to use an ODBC link for the view because it needs to be editable. Otherwise, I would have used a pass-through query. In previous discussions about using an MDB file vs. an ADP file as a front end for SQL Server, the impression I got was that...
2
1644
by: Roy Gourgi | last post by:
Hi, My program seems to slow down drastically because as I fill my array and table with many values, the program suffers tremendously. The first thing my program does is to search the jagged array to try to find an element in that array. If it does not find that element in that array, then it adds another element and that is the problem. Once I have many elements in that array, it takes a long time to do a search. Furthermore, not...
9
2072
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the way of providing const modifier for function parameters. From my point of view are, of course, design requirements always more important and thus one should always use const keyword with parameters whose values shouldn't get changed inside a...
2
6445
by: Mike Kober | last post by:
I am having issues with the File Upload control for sending files to the server via HTTP. The speed of the upload is often between 20kbs and 40kbs. If I use the LAN at work to the server, it runs over 2.3mbs. The slow speeds are when using multiple providers including RoadRunner and DSL. One test instance of a 40mb file showed it running at about 20kbs for about halfway, then it burst up to 500kbs for the rest of the file.
6
6263
by: Jassim Rahma | last post by:
I want to detect the internet speed using C# to show the user on what speed he's connecting to internet?
11
6507
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While this works in 95% of the time, we have some fringe cases where the only thing returned is the processor name. We use this data to help us decide which PCs need to be updated, so it would be nice to have the processor speed in all cases.
4
8632
by: nestle | last post by:
I have DSL with a download speed of 32MB/s and an upload speed of 8MB/s(according to my ISP), and I am using a router. My upload speed is always between 8MB/s and 9MB/s(which is above the max upload speed), ALWAYS. However, my download speed doesn't go over 25MB/s. And when my brother turns on the internet from his computer and takes up half the download/upload speeds (routers automatically split the speeds in two when two computers are using...
0
9977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11218
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10452
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7161
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4260
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.