Jump to content

Settimeout Question - Delayed Display Of Html.


JustKen
 Share

Recommended Posts

Hi all, I'd like to display a page of HTML after a short delay. My first approach, which works, was to enclose the HTML markup inside a document.write() function and call that function from setTimeout(), e.g. something like this:

t1 = setTimeout("writePage()", 10000);...function writePage() {document.write("<h1>Hello World!</h1>");}

But this can get quite complicated with a longer page with styling, as I'll have to escape all quote marks with a backslash, and also add line-continuation characters. And it really gets hairy if I want to include a navbar with its own document.write() functions within the HTML that's already nested within a document.write(). Is there a better way? Is there some way to implement a simple wait(n) function that just waits for n milliseconds before proceeding?

  • Like 1
Link to comment
Share on other sites

why not just use a outer element container, which uses display: none; then change it to display block after set time

<body><div id="wrapper"> rest of content...</div></body></div>

window.onload=function(){ x=document.getElementById("wrapper");x.style.display="none";t1 = setTimeout("showPage()", 10000);} function showPage() {document.getElementById("wrapper").style.display="block";}

if javascript is disabled the page will show straight away, else it will disappear, and reappear in 10 seconds.

Edited by dsonesuk
Link to comment
Share on other sites

Edit: It seems that somebody with a better approach answered while I was typing. You can freeze the browser for X amount of seconds by using an infinite loop. Why do you want to delay the page? There's probably a better approach.

function delay(seconds) {  var now = (new Date()).getTime();  var wait = seconds * 1000;  while( ( (new Date()).getTime() - now ) < wait) {}}delay(5);

Any code in the page after the delay() function will not appear until the time limit is up. If the delay is too long, the browser will ask the user if they want to stop the script.

Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...