0
我用window.open()做了一個空白窗口。
然後我用1秒的延遲添加了一個字符串「Wait a minute ...」3次。
就在添加字符串之前,我登錄了窗口的正文。使用「setTimeout()」時的執行順序是什麼?
窗口顯示字符串,如我所料,但控制檯日誌不是。
日誌
tmp.html:20 <body><h1>Wait a minute...</h1><h1>Wait a minute...</h1><h1>Wait a minute...</h1></body>
tmp.html:20 <body><h1>Wait a minute...</h1><h1>Wait a minute...</h1><h1>Wait a minute...</h1></body>
tmp.html:20 <body><h1>Wait a minute...</h1><h1>Wait a minute...</h1><h1>Wait a minute...</h1></body>
現在我不知道爲什麼結果發生了這樣的。
使用setTimeout()時,瀏覽器會忽略延遲並執行定時器功能,但不更新視覺事物的代碼除外。這樣對嗎?
代碼
<button onclick="func1()">Result</button>
<script>
\t function func1() {
\t \t var newWindow = window.open('about:Blank', 'newWindow',
\t \t \t 'width=480, height=272, resizable=1', true);
\t \t if (newWindow) {
\t \t \t var i = 3;
\t \t \t var func = function() {
\t \t \t \t var node = document.createElement('h1');
\t \t \t \t node.appendChild(document.createTextNode("Wait a minute..."));
\t \t \t \t console.log(newWindow.document.getElementsByTagName('body')[0]);
\t \t \t \t newWindow.document.getElementsByTagName('body')[0].appendChild(node);
\t \t \t \t if (--i > 0) {
\t \t \t \t \t setTimeout(func, 1000);
\t \t \t \t }
\t \t \t };
\t \t \t setTimeout(func, 1000);
\t \t } else {
\t \t \t window.alert('Popup Blocked');
\t \t }
\t }
</script>