Iterations using setTimeout with a timeout of 0 hang the browser

RESOLVED INVALID

Status

()

RESOLVED INVALID
7 years ago
7 years ago

People

(Reporter: weblivz, Unassigned)

Tracking

6 Branch
x86_64
Windows 7
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

7 years ago
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.5 Safari/535.2

Steps to reproduce:

Execute the following code:

<html>
	<head>
		<script>
			function doIteration() {
				for (i=0;i<10;i++) {
					writeOut(i);
					//writeOutDirect(i);				
				}

			}

			function writeOut(i) {
				setTimeout(function () {
					document.write(i+'<br/>');
				},0);
			}

			function writeOutDirect(i) {
				document.write(i+'<br/>');
			}
		</script>
	</head>
	<body onload="doIteration()">
		
	</body>
</html>


Actual results:

The first item in the iteration is written to the browser & it then hangs.


Expected results:

It should have written 0 to 9 in the browser, each on a new line.
Build identifier: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:9.0a1) Gecko/20110912 Firefox/9.0a1
Build ID: 20110912030829

Confirming the issue on latest nightly build: after the first iteration the browser hangs.
Status: UNCONFIRMED → NEW
Ever confirmed: true

Updated

7 years ago
Assignee: general → nobody
Component: JavaScript Engine → DOM
QA Contact: general → general
I don't see a browser hang here, I just see the spinner continue to spin indefinitely (bug 623880).  The browser is completely responsive for me.

I believe the expected behavior here is wrong.  The implicit document.open from the first document.write cancels all timeouts created by the script.  (http://www.whatwg.org/specs/web-apps/current-work/multipage/content-models.html#dom-document-open, step 11).
Status: NEW → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → INVALID
(Reporter)

Comment 4

7 years ago
Great. I found at higher iterations the browser would say "Not responding".

However using document.body.appendChild(t); in place of document.write works fine (a relief given my code needs that to work).

So it's the WebKit versions that are doing it wrong :-)

many thanks Kyle.
You need to log in before you can comment on or make changes to this bug.