有人可以解释为什么这段代码是按顺序执行的吗?
console.log('Main code, first line');
doThings(10000000, doThingsAfter);
function doThings(x,callback){
console.log('Am in doThings(x,callback), value received is ' + x);
for (var i= 0; i<x; i++){var minutes = new Date().getMinutes();} //wasting time here
console.log('Time after '+ x +' times was ' + minutes);
callback(minutes);
}
function doThingsAfter(minutes){
console.log('I am at doThingsAfter(), so I am the callback and I received minute:' + minutes);
}
console.log('Main code, last line');
结果是:
我希望
或至少
事情是最后console.log('Main code, last line');
不应该等到循环结束。我做错了什么?
谢谢!