You're seeing \n
create a new line because you're inside a <pre />
tag; normally you have to use a <br />
to see a similar new line outside of <pre />
.
When you call document.write
before the page has finished loaded, the output is entered inplace; so you'll see your FirstText \n SecText
written within the <pre />
.
However, when it's called after the page has loaded (within setInterval
), the existing page is cleared before the result is written; hence <pre />
is being removed, and you're not seeing your new line any more.
As you're not closing the document
using document.close()
, successive calls to document.write
in your setInterval
are adding to the document stream opened by the first iteration of setInterval
.
You can fix this by using a <br />
rather than \n
;
<script>
function text(){
document.write("FirstText <br /> SecText <br />");
}
setInterval(text, 1000);
</script>
For more info, see https://developer.mozilla.org/en-US/docs/Web/API/document.write and document.write clears page
: `document.write("FirstText
SecText");` – Utopik Jun 02 '13 at 19:38