Order of execution for console statements for different browsers

萝らか妹 提交于 2019-12-13 06:52:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!