setInterval and [removed] problem

后端 未结 4 1746
醉梦人生
醉梦人生 2021-01-22 21:13

I have this code

window.onload = function() {            
    function foo() {
        alert(\"test\");
    }
    setInterval(\"foo()\",500)
}

4条回答
  •  无人共我
    2021-01-22 21:49

    Using a string command in setInterval() will try to look for the function in the global (window) scope, but since the function is defined in a local scope, it won't be found. You should pass the function itself to setInterval() instead.

    window.onload = function() {            
        function foo() {
            alert("test");
        }
        setInterval(foo, 500);
    }
    

提交回复
热议问题