Javascript setInterval not working

后端 未结 4 1252
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 23:25

I need to run a javascript function each 10 seconds.

I understand the syntax must work like follow but I am not getting any success:

function funcNam         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 00:14

    That's because you should pass a function, not a string:

    function funcName() {
        alert("test");
    }
    
    setInterval(funcName, 10000);
    

    Your code has two problems:

    • var func = funcName(); calls the function immediately and assigns the return value.
    • Just "func" is invalid even if you use the bad and deprecated eval-like syntax of setInterval. It would be setInterval("func()", 10000) to call the function eval-like.

提交回复
热议问题