自动更新图标

时间:2013-05-06 21:09:12

标签: php javascript jquery mysql

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

我想要一些会检查数据库的时间并查看时间是否到了,显示图像1,如果没有显示图像2。

这是我到目前为止的代码:

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 个答案:

答案 0 :(得分: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通过添加auto.php参数向icon发送GET请求。 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 */

}

请告诉我这是否适合您。

答案 1 :(得分:1)

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

我很快就看到了:

attempt += 1

将其更改为

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

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

(++与+ = 1相同)

答案 2 :(得分: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元素的属性和get'em与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;

希望它有所帮助。

答案 3 :(得分: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]; ?>);