Reputation: 111
What I am currently trying to achieve is an auto icon updater. So far I only have it working for 1 icon, but I have 9. Now I have tried to repeat the same code 9 times, try to get it to work from the same file, ect... but to no success. Each icon has a separate timer which will show a different image. (Same image lower opacity)
I want a something which will check the database for the time and see if the time is up, show image 1 if not show image 2.
This is the code I have so far:
function runme() {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return false;
}
}
}
var str = "<?echo$id;?>";
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=<?echo$id;?>";
var strhehess = "&username=<?echo$name;?>";
ajaxRequest.open("GET", "auto.php?&id=" + str + strhehes + strhehess + strhehe, true);
ajaxRequest.send(null);
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState == 4) {
if (ajaxRequest.status == 200) {
attempt = 0;
document.getElementById("icon_messaging").innerHTML = ajaxRequest.responseText;
document.getElementById("error_mess").innerHTML = '';
document.getElementById("error_mess").style.display = 'none';
} else {
attempt += 1
document.getElementById("error_mess").style.display = 'block';
document.getElementById("error_mess").innerHTML = '<br><font color="#ff4040" onMouseover="ddrivetip(\'There is an error connecting. The game will continue trying to connect again.\')" onMouseout="hideddrivetip()" style="cursor: pointer;">Error Code: ' + new XMLHttpRequest().status + '<br>Attempts: ' + attempt + '</font>';
}
}
}
setTimeout("runme()", 6000);
}
setTimeout("runme()", 5000);
Here is auto.php:
//AUTO INCLUDE
$userids = $_GET['userid'];
$saturate = "/[^a-z0-9]/i";
$saturatesd = "/[^0-9]/i";
$sessionid = preg_replace($saturate,"",$sessionidbefore);
$userid = preg_replace($saturatesd,"",$userids);
$statustest = mysql_query("SELECT newmail,lastactive FROM login WHERE id = '$userids' LIMIT 1");
$statustesttwo = mysql_fetch_array($statustest);
$mails = $statustesttwo['newmail'];
$last_active_1 = $statustesttwo['lastactive'];
if($mails == '0'){
echo "<a id='inboxspan' href='/home.php?pageid=80'><img src='images/mail-yes.gif' style='border-style: none'></a>";
}else{
echo "<a id='inboxspan' href='/home.php?pageid=80'><img src='images/layout/mail-n.jpg' style='border-style: none'></a>";
}
Upvotes: 1
Views: 317
Reputation: 726
function runme(icon) {
var iconElementId;
var iconTimer;
switch (icon) {
case "mail":
iconElementId = "icon_messaging";
iconTimer = 5000;
break;
case "gta":
iconElementId = "gta_icon";
iconTimer = <? echo $icon_secs[0]; ?>;
break;
case "burg":
iconElementId = "c_icon";
iconTimer = 5000;
break;
case "crimes":
iconElementId = "crimes_icon";
iconTimer = <? echo $icon_secs[1]; ?>;
break;
case "chase":
iconElementId = "chase_icon";
iconTimer = <? echo $icon_secs[2]; ?>;
break;
case "robbery":
iconElementId = "robbery_icon";
iconTimer = <? echo $icon_secs[3]; ?>;
break;
case "train":
iconElementId = "train_icon";
iconTimer = <? echo $icon_secs[4]; ?>;
break;
case "goods":
iconElementId = "goods_icon";
iconTimer = <? echo $icon_secs[5]; ?>;
break;
case "df":
iconElementId = "df_icon";
iconTimer = <? echo $icon_secs[6]; ?>;
break;
case "sm":
iconElementId = "sm_icon";
iconTimer = <? echo $icon_secs[7]; ?>;
break;
}
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return false;
}
}
}
var str = "<?echo $id;?>";
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=<?echo $id;?>";
var strhehess = "&username=<?echo $name;?>";
ajaxRequest.open("GET", "auto.php?icon=" + encodeURIComponent(icon) + "&id=" + str + strhehes + strhehess + strhehe, true);
ajaxRequest.send(null);
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState == 4) {
if (ajaxRequest.status == 200) {
attempt = 0;
document.getElementById(iconElementId).innerHTML = ajaxRequest.responseText;
document.getElementById("error_mess").innerHTML = '';
document.getElementById("error_mess").style.display = 'none';
} else {
attempt += 1
document.getElementById("error_mess").style.display = 'block';
document.getElementById("error_mess").innerHTML = '<br><font color="#ff4040" onMouseover="ddrivetip(\'There is an error connecting. The game will continue trying to connect again.\')" onMouseout="hideddrivetip()" style="cursor: pointer;">Error Code: ' + new XMLHttpRequest().status + '<br>Attempts: ' + attempt + '</font>';
}
}
}
setTimeout("runme('" + icon + "')", iconTimer);
}
setTimeout("runme('mail')", 5000);
setTimeout("runme('gta')", <? echo $icon_secs[0]; ?>);
setTimeout("runme('burg')", 5000);
setTimeout("runme('crimes')", <? echo $icon_secs[1]; ?>);
setTimeout("runme('chase')", <? echo $icon_secs[2]; ?>);
setTimeout("runme('robbery')", <? echo $icon_secs[3]; ?>);
setTimeout("runme('train')", <? echo $icon_secs[4]; ?>);
setTimeout("runme('goods')", <? echo $icon_secs[5]; ?>);
setTimeout("runme('df')", <? echo $icon_secs[6]; ?>);
setTimeout("runme('sm')", <? echo $icon_secs[7]; ?>);
Upvotes: 1
Reputation: 4383
If I understood your question correctly, this is the updating system for the "new mail" icon, and you need to check and update other stuff too. Since you want separate timers, you could parametrize the runme()
function. Your JavaScript could be modified like this:
function runme(icon) {
var iconElementId;
var iconTimer;
switch (icon) {
case "mail":
iconElementId = "icon_messaging";
iconTimer = 6000;
break;
case "news":
iconElementId = "icon_notifications"; // I'm making up names and timeouts here
iconTimer = 3000;
break;
case "something":
iconElementId = "icon_something"; // Still making up
iconTimer = 8000;
break;
/* And so on, covering all your 9 cases */
}
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return false;
}
}
}
var str = "<?echo $id;?>";
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=<?echo $id;?>";
var strhehess = "&username=<?echo $name;?>";
ajaxRequest.open("GET", "auto.php?icon=" + encodeURIComponent(icon) + "&id=" + str + strhehes + strhehess + strhehe, true);
ajaxRequest.send(null);
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState == 4) {
if (ajaxRequest.status == 200) {
attempt = 0;
document.getElementById(iconElementId).innerHTML = ajaxRequest.responseText;
document.getElementById("error_mess").innerHTML = '';
document.getElementById("error_mess").style.display = 'none';
} else {
attempt += 1;
document.getElementById("error_mess").style.display = 'block';
document.getElementById("error_mess").innerHTML = '<br><font color="#ff4040" onMouseover="ddrivetip(\'There is an error connecting. The game will continue trying to connect again.\')" onMouseout="hideddrivetip()" style="cursor: pointer;">Error Code: ' + new XMLHttpRequest().status + '<br>Attempts: ' + attempt + '</font>';
}
}
}
setTimeout(function(){runme(icon);}, iconTimer);
}
setTimeout(function(){runme("mail");}, 5000);
setTimeout(function(){runme("news");}, 5000);
setTimeout(function(){runme("something");}, 5000);
/* And so on */
So, now your JavaScript sends a GET request to auto.php
with the addition of the icon
parameter. The PHP script will have to manage that, too.
//AUTO INCLUDE
$icon = urldecode($_GET['icon']);
$userids = $_GET['userid'];
$saturate = "/[^a-z0-9]/i";
$saturatesd = "/[^0-9]/i";
$sessionid = preg_replace($saturate,"",$sessionidbefore);
$userid = preg_replace($saturatesd,"",$userids);
switch($icon) {
case "mail":
$statustest = mysql_query("SELECT newmail,lastactive FROM login WHERE id = '$userids' LIMIT 1");
$statustesttwo = mysql_fetch_array($statustest);
$mails = $statustesttwo['newmail'];
$last_active_1 = $statustesttwo['lastactive'];
if ($mails == '0') {
echo "<a id='inboxspan' href='/home.php?pageid=80'><img src='images/mail-yes.gif' style='border-style: none'></a>";
} else {
echo "<a id='inboxspan' href='/home.php?pageid=80'><img src='images/layout/mail-n.jpg' style='border-style: none'></a>";
}
break;
case "news":
$statustest = mysql_query("SOME OTHER SQL QUERY");
$statustesttwo = mysql_fetch_array($statustest);
/* check whatever you need to */
if (/* something */) {
echo "the HTML for the icon";
} else {
echo "the HTML for the other icon ";
}
break;
/* And so on, again, covering all your 9 cases */
}
Let me know if this works for you.
Upvotes: 2
Reputation: 7900
Is this function runme
generated by PHP?
var str = "<?echo$id;?>";
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=<?echo$id;?>";
var strhehess = "&username=<?echo$name;?>";
Because if it is not, there is no way this code will work, because Javascript cannot interpret PHP.
In this case, you should put this as attributes of HTML elements and get'em with DOM. When generating your HTML with PHP, do:
echo '<output id="data-id">' . $id . '<output>';
echo '<output id="data-user-id">' . $id . '<output>';
echo '<output id="data-user-name">' . $username . '<output>';
You can hide this elements with CSS. So then in your Javascript, you should do:
var str = document.getElementById('data-id').innerHTML;
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=" + document.getElementById('data-user-id').innerHTML;
var strhehess = "&username=" + document.getElementById('data-user-name').innerHTML;
Hope it helps.
Upvotes: 1
Reputation: 11832
I don't know what your exact problem is. Do you get an error?
What I quickly see is:
attempt += 1
Change this into
if (typeof attempt == "undefined") attempt = 0;
attempt ++;
So add the semi-colon ;
, and first check if the var already exists
( ++ is the same as += 1 )
Upvotes: 1