我有一個關於JavaScript函數序列的問題,下面我有兩個代碼,爲什麼這兩個方案的不同的結果? 我認爲第一個程序結果將等於第二個程序結果。javascript函數的執行順序
function test1() {
for (var i = 1; i <= 1000000; i++) {
}
console.log("test1");
}
function test2() {
console.log("test2");
}
test1();
test2();
//test1
//test2
function test1() {
setTimeout(function() {
console.log("test1");
}, 1000);
}
function test2() {
console.log("test2");
}
test1();
test2();
//test2
//test1
運行的循環是同步,'setTimeout'是異步,這就是區別 – Thomas