Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript time delay 1

Status
Not open for further replies.

intrivious

Programmer
Jan 17, 2006
6
US
All I need is a script telling the browser to print a string to the screen every x seconds. Is this code incorrect?

Code:
<html>
<head>
<script type="text/javascript">

function init() {
  document.write("blah blah blah<br>");
  setTimeout("init()", 1000);
}

</script>
</head>
<body onload='init()'>
</body>
</html>

Why does this not work? It displays the string once, then on the next second, it throws an error saying 'missing object'?

Thanks in advance ;)
 
It's the document.write that's killing it. Try the same code with an alert instead of document.write and you'll see it works fine.

instead of using document.write, you could consider any of the DOM manipulation techniques, such as adding child paragraphs.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Thanks a lot for the help. I have not tried it at the moment, but document.write is all I have been trying to test it with just to understand how setTimeout works. Appreciate it ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top