2013-05-06 57 views
1

我目前试图实现的是自动图标更新程序。到目前为止,我只能为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>"; 
} 
+0

请显示'auto.php'的代码。 – 2013-05-06 21:20:53

+0

已添加auto.php。 – 2013-05-06 21:40:49

+1

你确切的问题是什么?您是否在开发人员工具中每隔5秒或6秒看到一个请求?你看到任何错误被提出?到底发生了什么,什么不发生? – 2013-05-16 11:24:46

回答

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]; ?>); 
+0

嗯,这正是我的答案,没有PHP部分和解释。没有进攻(只要它对OP有好处,对每个人都有好处),但接受这个是一个奇怪的举动:D – Aioros 2013-05-20 07:18:00

+0

这正是他需要的,所以我把它放在这里来得到正确的答案。 – 2013-05-20 11:29:13

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

} 

让我知道这是否适合你。

+0

谢谢你的回答。 encodeURIComponent(icon)是做什么的? – 2013-05-16 12:22:33

+0

这是一个函数,它对字符串进行编码,以便将其用作URL中的参数。白色空间变成%20,依此类推。 PHP脚本具有相应的'urldecode()'来获取原始字符串。顺便说一下,我添加了一些我忘记的单引号,这要归功于nl-x的提示。 – Aioros 2013-05-16 13:18:47

+0

我明白了。顺便说一句,我刚刚试过这与第一个图标,它只更新第一次,但不更新第二次? – 2013-05-16 13:49:45

1

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

我很快看到的是:

attempt += 1 

变成

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

此所以加分号;,并首先检查了var已经存在

(++与+ = 1相同)

+0

@Sam Ham,浏览器控制台是否显示任何错误? – 2013-05-16 16:11:49

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进行get'em。 当生成与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; 

希望它有帮助。

+0

是的,它是由PHP生成的。 – 2013-05-16 12:02:25