问题
I've tested the following code in both Firefox (32) and Chrome (38) so far
var startTime = (new Date()).getTime();
console.log(startTime);
var data = [];
var tCat = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
var dataPoints = 2000000;
for ( var i = 0; i < dataPoints; i++ ) {
var r = Math.random;
data.push({
value: Math.pow(1 - Math.cos((r() * Math.PI) / 2), 3),
cost: +r().toFixed(2),
category: tCat[Math.floor(r() * tCat.length)]
});
}
var endTime = (new Date()).getTime();
console.log(endTime);
console.log(endTime - startTime);
You can view it at this jsfiddle
In Firefox, it seems all three console statements fire at the same time, after the loop finishes execution, but in Chrome the first console statement is shown before the loop begins execution. The code itself works as intended but the execution of the first console statement in Firefox is bewildering. Can anyone explain this?
回答1:
TL;DR: Consoles are not governed by any standard and depend on the browser.
It works as expected. Try adding breakpoints and step through each line in the code, and you'll see that they execute as expected. A much more crude approach is paralyze your script with alert()
.
However, behavior is still vendor-dependent. No guarantees.
来源:https://stackoverflow.com/questions/26426397/order-of-execution-for-console-statements-for-different-browsers