调用函数setBlinkFont()时未捕获的Referenceerror

时间:2016-02-02 03:36:47

标签: javascript jquery

我们正试图在收到java应用程序传入的车辆号码时实现闪烁效果。但是,当我们调用该函数时,我们收到了

  

未捕获的Referenceerror:未定义setBlinkFont()。

任何人都可以帮助解决此错误?

<%-- 
        Document   : rightScreen
        Created on : Jan 6, 2016, 9:09:37 AM
        Author     : 130876R
    --%>

    <%@page import="java.text.SimpleDateFormat"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="Model.AlprList"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>

    <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>

        <body onload="start();">
            <script type="text/javascript">
                function start() {

                    var eventSource = new EventSource("RetrieveResults");

                    eventSource.onmessage = function(event) {

                        //whatever you write in the servlet, you get it out using event.data. 
                        ////Which means, data is actually the changes you want.
                        var vehicleObject = JSON.parse(event.data);
                        var statusColor = document.getElementById("statusColor");
                        console.log("vehicle number : " + vehicleObject.vehicleNumber);
                        console.log("image : " + vehicleObject.image);
                        console.log("vvipStatus:" + vehicleObject.vvipStatus)
                        if (vehicleObject.vehicleNumber != null) {
                            blinkFont()
                        }
                        function setblinkFont()
                        {
                            document.getElementById(vehicleObject.vehicleNumber).style.color = "blue"
                            setTimeout("blinkFont()", 1000)
                        }

                        function blinkFont()
                        {
                            document.getElementById(vehicleObject.vehicleNumber).style.color = "red"
                            setTimeout("setblinkFont()", 1000)
                        }


                    };


                }


            </script>

            <%
                Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/fyp", "root", "password");
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
                String currentDate = sdf.format(cal.getTime());

                Statement statement = connection.createStatement();
                ResultSet resultset
                        = statement.executeQuery("select * from alprlist where DateOfArrival = '" + currentDate + "' ");


            %>
            <% while (resultset.next()) {%>
            <span id="<%= resultset.getString(8)%>"> <%= resultset.getString(8)%> </span><br>
            <% }%>


        </body>
    </html>

1 个答案:

答案 0 :(得分:2)

由于setTimeout("blinkFont()", 1000),当您将字符串作为setTimeout回调传递时,它会在全局范围内进行评估 - 它的评估与eval()相同,但是您有在闭包范围中定义了函数blinkFontsetblinkFont。所以只需将函数引用传递给setTimeout()

setTimeout(blinkFont, 1000)
setTimeout(setblinkFont, 1000)