javascript通知检查程序脚本未捕获的参考错误

时间:2016-05-09 19:31:03

标签: javascript php

我有这个通知检查器,我不知道这个js代码有什么问题,我在checkMessage()上有错误:未捕获的参考错误:未定义checkMessage

js代码:

function checkMessage(){

if (window.XMLHttpRequest) 
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function() //when the request is submitted...
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200 //...and everything is ok...
    {
        if(xmlhttp.responseText>0){ // if the response text is greater than 0

            document.title="("+xmlhttp.responseText+")"+"My Social Network - Welcome"; 

            document.getElementById('checkMsg').innerHTML="<b>" + xmlhttp.responseText+" Notifications</b>";
        }
        else 
            document.title="My Social Network - Welcome";

            document.getElementById('checkMsg').innerHTML="No notifications";
    }
  }

xmlhttp.open("GET","checkMessage.php?user_id="+"1",true); 
xmlhttp.send();
}

checkmessage.php代码:

<?php
        include 'dbm.php'; //database settings from an external file

        $user = $_SESSION['user'];

        $get_count_messages = mysql_query("SELECT message FROM notifications ");
        $row = mysql_num_rows($get_count_messages); // count how many rows the query returned

        echo "$row"; //return the value
?>

html代码:

<html>
    <head>

        <script type="text/javascript" src="notifications.js"></script>
        <script type="text/javascript">
            setInterval("checkMessage()", 1000);
        </script>
    </head>
    <body>
        <div id="checkMsg"></div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为您的setInterval功能应如下所示:

setInterval(checkMessage, 1000);

因为setInterval期望函数作为参数。

答案 1 :(得分:0)

这只是一个简单的例子,可以在通知数量之间产生差异

 if (xmlhttp.readyState==4 && xmlhttp.status==200 //...and everything is ok...
 {    
     var title = "";
     var message;
     if(xmlhttp.responseText > 0)
     {
        title += "(" + xmlhttp.responseText + ")";
        if(xmlhttp.responseText > 1)
           message = "<b>" + xmlhttp.responseText + " Notifications</b>";
        else
           message = "<b>" + xmlhttp.responseText + " Notification</b>";
     }
     else
         message = "No notifications";

     title += "My Social Network - Welcome";

     document.title = title; 
     document.getElementById('checkMsg').innerHTML= message;
 }