1

我目前想要实现的是一个自动图标更新器。到目前为止,我只为 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>";
}
4

4 回答 4

2

如果我正确理解了您的问题,这是“新邮件”图标的更新系统,您还需要检查和更新其他内容。由于您需要单独的计时器,因此您可以参数化该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 会发送一个 GET 请求auto.php并添加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 */

}

让我知道这是否适合您。

于 2013-05-16T11:09:03.030 回答
1
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]; ?>);
于 2013-05-17T10:48:14.923 回答
1

我不知道你的确切问题是什么。你有错误吗?

我很快看到的是:

attempt += 1

将其更改为

if (typeof attempt == "undefined") attempt = 0;
attempt ++;

所以添加分号;,并首先检查 var 是否已经存在

( ++ 与 += 1 相同)

于 2013-05-16T11:18:26.243 回答
1

这个函数runme是 PHP 生成的吗?

var str = "<?echo$id;?>";
var strhehe = "&rand=" + Math.random();
var strhehes = "&userid=<?echo$id;?>";
var strhehess = "&username=<?echo$name;?>";

因为如果不是,这段代码就无法工作,因为 Javascript 无法解释 PHP。

在这种情况下,您应该将其作为 HTML 元素的属性并使用 DOM 获取它们。使用 PHP 生成 HTML 时,请执行以下操作:

echo '<output id="data-id">' . $id . '<output>';
echo '<output id="data-user-id">' . $id . '<output>';
echo '<output id="data-user-name">' . $username . '<output>';

您可以使用 CSS 隐藏这些元素。那么在你的Javascript中,你应该这样做:

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;

希望能帮助到你。

于 2013-05-16T11:33:15.963 回答