setTimeout - how to avoid using string for callback?

后端 未结 2 1318
长发绾君心
长发绾君心 2020-12-09 05:48

When using setTimeout, you have to put the code you want to execute into a string:

setTimeout(\'alert(\"foobar!\");\', 1000);

2条回答
  •  攒了一身酷
    2020-12-09 06:02

    Who said that it doesn't let you do it?

    It does, the code -

    setTimeout(function() { myFunction(); }, 1000);
    

    is perfectly valid.

提交回复
热议问题