简体   繁体   English

为什么console.log运行无限循环直到溢出,而document.write不运行

[英]Why does console.log run infinite loops until overflow, and document.write not

In javascript, if you have an infinite loop using console.log, it will write everything to the console until it overflows. 在javascript中,如果使用console.log有无限循环,它将把所有内容写入控制台,直到溢出为止。 However, if you do this with document.write, in an infinite loop, it will freeze the page and nothing will load. 但是,如果使用document.write进行此操作,则会无限循环地冻结页面,并且不会加载任何内容。 Does anyone know the reason for this? 有人知道原因吗?

example is here 例子在这里

<html>
<head>
    <script type='text/javascript'>
    var x = 0;

    function conslog() {
        var x = 0;
        while (1) {
            console.log(x);
            x++;
        }
    }

    function dowrite() {
        var x = 0;
        while (1) {
            document.write(x);
            x++;
        }
    }
    </script>
</head>
<body>
    <button type='button' onClick='conslog()'>console</button>
    <button type='button' onClick='dowrite()'>write</button>
</body>
</html>

http://shodor.org/~amalani/infinite.html http://shodor.org/~amalani/infinite.html

The view process for logged messages is independent of the script. 日志消息的查看过程与脚本无关。 While the script runs in an infinite loop, at some times the renderer will intercept it and show the queued messages. 当脚本无限循环运行时,渲染器有时会拦截它并显示排队的消息。

In contrast, document.write does write to a buffer that is going to be parsed when the pending parsing-blocking script finished running. 相比之下, document.write确实会写入待处理的解析阻止脚本完成运行时要解析的缓冲区。 Only it doesn't finish… 只是还没有结束…

If you'd use the DOM to append new elements, they would probably output. 如果使用DOM附加新元素,则可能会输出它们。

The page will repaint when Javascript is not executing, if it's running a loop the page won't repaint because Javascript is still executing. 当不执行Javascript时,页面将重新绘制,如果运行循环,则由于Javascript仍在执行,页面将不会重新绘制。 The console may repaint while Javascript is running on the page. 当页面上运行Javascript时,控制台可能会重新绘制。

Mostly likely because there are no finite limits on the size of the HTML page, but the console debug log will? 最有可能是因为HTML页面的大小没有限制,但是控制台调试日志会吗? I'm guessing your CPU load went up as well whilst the browser effectively did nothing more than process a loop. 我猜想您的CPU负载也会增加,而浏览器实际上只是处理循环而已。 I'm guessing this is with code that's something of the form while (1) { console.log ("something") }; 我猜这是代码的形式, while (1) { console.log ("something") }; ?

You've not stated what browser this is with, so you'll probably find different browsers have different behaviours of the console log. 您尚未说明这是什么浏览器,所以您可能会发现不同的浏览器具有不同的控制台日志行为。 Those that implement it as a growing list or ring buffer may also suffer the browser hanging as well. 那些将其实现为不断增长的列表或环形缓冲区的人,也可能会遭受浏览器的挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 document.write不起作用,但console.log起作用 - document.write doesnt work, but console.log does Console.log与document.write - Console.log vs document.write 为什么document.write和console.log为getElementById提供不同的输出? - Why does document.write and console.log give different outputs for getElementById? 为什么console.log()和document.write()在JavaScript中为二维数组输出不同的结果? - Why does console.log() and document.write() output different results for a two-dimensional array in JavaScript? document.write与console.log不同? - document.write different from console.log? console.log / document.write和alert之间的区别 - difference between console.log / document.write and alert document.write vs console.log vs innerHTML - document.write vs console.log vs innerHTML 在javavascript提示(),document.write()和console.log()中使用“ +”(加号)和“,”(逗号) - Using the “+” (plus) and the “,” (comma) in javavascript prompt(), document.write() and console.log() 使用 console.log 或 document.write 将转换后的字符串显示为数字无效。 串联显示不工作也不工作 - Displaying converted string to number using console.log or document.write not working. Concatenation display not working not working either 为什么 alert(); 在 console.log() 之前运行; - Why does alert(); run before console.log();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM