birdbrain24
03-31-2008, 01:21 AM
I was wondering if there was anyway that i could only tell the ajax to update when the mysql changes. Heres my script, i was using setTimeout but it really is a cpu hog being played over and over!
<?php ...
if($xmlhttp == 'true'){
$update = $_POST['update'];
if($update == 'true'){
echo '<strong>Username:</strong> ' . $users['username'] . ' <strong>Rank:</strong> ' . $users['rank'] . ' <strong>Cash:</strong> $' . number_format($users['cash']) . ' <strong>Vehicle:</strong> ' . $selected . ' <strong>Location:</strong> ' . $users['location'] . ' <strong>Reputation:</strong> ' . $users['reputation'];
}
} else {
?>
<!DOCTYPE ...
<head>
<script language="javascript" type="text/javascript">
function UpdateUserbar(){
var xmlhttp;
try{
xmlhttp = new XMLHttpRequest();
} catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
alert("Your browser do not support our technology!");
return false;
}
}
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
document.getElementById('userbar').innerHTML = xmlhttp.responseText;
setTimeout('UpdateUserbar();', 1);
}
}
var params = "xmlhttp=" + "true" + "&" + "update=" + "true";
var url = "userbar.php";
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(params);
}
</script>
</head>
<body onload="UpdateUserbar();">
<div id="userbar"><!-- Userbar Here! --></div>
</body>
</html>
<?php
}
?>
<?php ...
if($xmlhttp == 'true'){
$update = $_POST['update'];
if($update == 'true'){
echo '<strong>Username:</strong> ' . $users['username'] . ' <strong>Rank:</strong> ' . $users['rank'] . ' <strong>Cash:</strong> $' . number_format($users['cash']) . ' <strong>Vehicle:</strong> ' . $selected . ' <strong>Location:</strong> ' . $users['location'] . ' <strong>Reputation:</strong> ' . $users['reputation'];
}
} else {
?>
<!DOCTYPE ...
<head>
<script language="javascript" type="text/javascript">
function UpdateUserbar(){
var xmlhttp;
try{
xmlhttp = new XMLHttpRequest();
} catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
alert("Your browser do not support our technology!");
return false;
}
}
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
document.getElementById('userbar').innerHTML = xmlhttp.responseText;
setTimeout('UpdateUserbar();', 1);
}
}
var params = "xmlhttp=" + "true" + "&" + "update=" + "true";
var url = "userbar.php";
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(params);
}
</script>
</head>
<body onload="UpdateUserbar();">
<div id="userbar"><!-- Userbar Here! --></div>
</body>
</html>
<?php
}
?>