在setTimeout函数不起作用

有两个函数hello1()和hello2()。

function hello1(){ console.log('hello1'); } function hello2(){ console.log('hello2'); } setTimeout(hello1, 3000); setTimeout(hello2(), 3000); 

setTimeout(hello1, 3000); ,延迟3秒后打印“hello1”。

但在setTimeout(hello2(), 3000); ,立即打印“hello2”。

我想这是因为它必须在setTimeout中使用函数名。

如果我想像hello(1)那样在延迟3秒之后用参数执行一个函数呢?

因为我想传递参数到函数中,所以我不能只使用setTimeout中的函数名,比如setTimeout(hello1, 3000);

setTimeout中为函数使用括号时,会立即执行该函数。

要使用带参数的函数,可以使用任意函数作为超时函数,并在其中调用函数。

 setTimeout(function() { hello(1, 'param'); }, 3000);