Javascript delay problem... help please!

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Javascript delay problem... help please!

Post by Citizen »

If I use this:

Code: Select all

<script>
document.writeln('<p>What ever you want to write<\/p>');
</script>
In my html code, it prints fine like normal text.

But.... if I use this to create a delay before writing the text, it creates a new blank page with only the written text:

Code: Select all

<script>
setTimeout(\"document.write('<p>What ever you want to write<\/p>')\",500);
</script>

Is there any way for me to put a delay before writing the text without it creating a new page?

Thanks!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ahh that'll be DOM scripting. What you'll want to do is put your code inside

Code: Select all

window.onload = function() { /* here */ }
and use DOM traversal to find the point where you want to insert the paragraph. and then use things like

Code: Select all

document.createElement('P') and someNode.appendChild()
You really have to learn it though. However if you pick up a JavaScript framework you may find specific documentation on how to achieve this using that framework.

In addition I don't think you can use setTimeout like that. This is the correct usage

Code: Select all

function timeoutHandler()
{
   // code
}
setTimeout(timeoutHandler, 500);
Post Reply