Javascript 中 settimeout 的使用
Usage of setimeout in Javascript
以下程序的输出是什么,为什么?
setTimeout(function() {
console.log("hi");
}, 0);
//Do some calculation for 3-4 secs or more over here
console.log("Bye");
我无法模拟,因为我不确定如何进行 3-4 秒的计算。另外,如果值是 Bye 而不是 Hi,那是为什么?
输出是
1.Bye
2.hi
为什么?即使超时为 0 毫秒,该函数也会由 setTimeout 提供给调用堆栈,但在此之前,调用堆栈的末尾已经有 console.log("Bye")。这就是为什么它被更早执行的原因。
以下程序的输出是什么,为什么?
setTimeout(function() {
console.log("hi");
}, 0);
//Do some calculation for 3-4 secs or more over here
console.log("Bye");
我无法模拟,因为我不确定如何进行 3-4 秒的计算。另外,如果值是 Bye 而不是 Hi,那是为什么?
输出是 1.Bye 2.hi
为什么?即使超时为 0 毫秒,该函数也会由 setTimeout 提供给调用堆栈,但在此之前,调用堆栈的末尾已经有 console.log("Bye")。这就是为什么它被更早执行的原因。