Jquery Web workers只返回一条消息,之后不返回任何消息,尽管使用了setTimeout

Jquery Web workers只返回一条消息,之后不返回任何消息,尽管使用了setTimeout,jquery,html,Jquery,Html,我编写了一些代码,用于从外部js文件(workers_demo.js)接收web工作者消息,并将其放在输出标记之间。javascript是这样的:单击按钮时接收第一条消息,并且每秒接收一条消息(因此标记之间的html每秒增加1条)。但是,问题是数字会增加到1,但此后不会继续增加。以下是主文件: <!DOCTYPE html> <html> <head> <script type="text/javascript" src= "jquery-2.1

我编写了一些代码,用于从外部js文件(workers_demo.js)接收web工作者消息,并将其放在输出标记之间。javascript是这样的:单击按钮时接收第一条消息,并且每秒接收一条消息(因此标记之间的html每秒增加1条)。但是,问题是数字会增加到1,但此后不会继续增加。以下是主文件:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src= "jquery-2.1.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            if(typeof(Worker) !== "undefined"){
                var w;
                $("button#start").click(function(){
                    if(typeof(w) == "undefined") {
                        w = new Worker("workers_demo.js");
                    } 
                    w.onmessage = function(event){
                        console.log("I have received a message"); 
                        $("output#number").html(event.data);
                    }
                });
                $("button#stop").click(function(){
                    w.terminate();
                });
            }else{
                //fallback code
                $("output#number").html("Sorry no web workers.");
            }
        });
    </script>
</head>
<body>
    <output id="number"></output>
    <button id = "start">Start Counting</button>
    <button id = "stop">Stop Counting</button>
</body>
</html>

是什么导致这个数字第一次增加而不是随后增加的?

我已经在评论中说过了,但我会回答这个问题,让人们知道这个问题有答案:

setTimeout("increment_numbers",1000);
应该是

setTimeout(increment_numbers,1000); //no quotes around the function name

松开
setTimeout
中的引号:更改
setTimeout(“增量_数”,1000)
设置超时(增量为1000)将函数引用传递给setTimeout,而不是字符串。传递的字符串作为JS进行计算,但字符串中的代码不做任何事情(它不调用函数)。
setTimeout(increment_numbers,1000); //no quotes around the function name