gpt4 book ai didi

php - 自动更新图标

转载 作者:行者123 更新时间:2023-11-29 04:23:42 27 4
gpt4 key购买 nike

我目前正在努力实现的是一个自动图标更新程序。到目前为止,我只让它为 1 个图标工作,但我有 9 个。现在我已经尝试将相同的代码重复 9 次,尝试让它在同一个文件中工作,等等......但没有成功。每个图标都有一个单独的计时器,它将显示不同的图像。 (相同的图像降低不透明度)

我想要一个可以检查时间数据库的东西,看看时间是否到了,如果没有显示图像 2,则显示图像 1。

这是我目前的代码:

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);

这里是 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>";
}

最佳答案

如果我对你的问题理解正确,这是“新邮件”图标的更新系统,你还需要检查和更新其他内容。由于您需要单独的计时器,因此可以参数化 runme() 函数。您的 JavaScript 可以这样修改:

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 */

因此,现在您的 JavaScript 向 auto.php 发送了一个 GET 请求,并添加了 icon 参数。 PHP 脚本也必须管理它。

//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 */

}

让我知道这是否适合您。

关于php - 自动更新图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16407312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com