Here is a simple program that prints numbers one to ten on the browser window.
In addition to the points by T.J. Chowder about the webkit bug, consider these two scripts and why your code specifically doesn't work like you may have expected.
foo
foo
In the first, setInterval() is triggering again after the page is closed. You'll see the alert message indicating the page had executed fully, and the document is closed for writing. After that happens, you will see the document.write being triggered by the first interval. This unloads the old, previously closed document, and adds only 1 to the page.
Since the old document has been unloaded, the existing javascript is gone and there is nothing else to execute beyond the first iteration.
In the second script, document.write is executed multiple times while the current document is still open. Since the document.write functions are executed before the document is closed naturally, the original contents of the document are never overwritten.
Its better to interact with DOM elements directly than to use document.write.
There is some more good reading on it here: https://pigeoto.wordpress.com/2011/01/19/why-doesnt-document-write-work-with-setinterval/