我有一个关于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