如果我使用setTimeout()和setInterval()调用命名函数而不使用括号,它将按预期工作.当我用括号调用相同的函数时,它会立即执行它或者给出错误.
我正在寻找一个更深入的了解这个问题,然后我在网上找到了什么.你们能告诉我为什么这是真的吗?
var func = function(){
console.log("Bowties are cool.");
}
setTimeout(func(), 1500);
// Prints "Bowties are cool." immediately
setInterval(func(), 1500);
// Throws an error
setInterval(func, 1500);
// Works as expected
setTimeout(console.log("Bowties are cool."),1500);
// This method has the same result as "setTimeout(func(), 1500)".
Run Code Online (Sandbox Code Playgroud) javascript ×1