setInterval() and setTimeout don't work with document.write

Is there a way to make setInterval() and setTimeout() call document.write repeatedly? It seems like document.write isn't supported by setInverval and setTimeout.

For example, these scripts don't work:

function loopFun() {
  document.write("Here we go again...<br/>");
   setTimeout("loopFun()", 5000);
   }
loopFun();

function loopFun() {
  document.write("Here we go again...<br/>");
     setInterval("loopFun()", 5000);
}
loopFun();

But if you replace document.write() with alert(), the scripts work:

function loopFun(){
alert("Here we go again");
setTimeout("loopFun()", 5000);
}
loopFun();

function loopFun(){
alert("Here we go again");
setInterval("loopFun()", 5000);
}
loopFun();
MBaronganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
This page from W3Schools shows different ways to use those functions: http://www.w3schools.com/js/js_timing.asp
0
MBaronganAuthor Commented:
I looked through all the examples from http://www.w3schools.com/js/js_timing.asp, but I don't see document.write in any of them (unless I overlooked it). Does that mean document.write can't be used with setInterval and setTimeout?
0
Dave BaldwinFixer of ProblemsCommented:
I didn't have any problem getting 'document.write' to work twice... but the timing loop failed after the second time.  Note that the function with 'document.write' must be in the body of the page.  'alert' will work in the <head> section but 'document.write' will not.

Try the examples on that page and see if they work for you as they are.  Note that setTimeout is shown as working once while setInterval is shown as being repetitive.  I suspect that your 'setInterval' isn't going to work right because you're setting a repetitive interval over and over again without ever shutting any of the intervals down.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
In layman's terms, what do you want to make happen?  document.write() is kind of "hamfisted."  Have you tried putting the interval timers outside of the function definition?
0
leakim971PluritechnicianCommented:
put a div (or maybe a <p></p>...) in the page where you want to see it write.

<div id="writeHere"></div>

and use the following code :
Test page : http://jsfiddle.net/m54hg/1/

function loopFun() {
  var container = document.getElementById("writeHere");
  var currentContent = container.innerHTML;
  var newContent = currentContent + "Here we go again...<br/>";
  container.innerHTML = newContent;
  setTimeout("loopFun()", 5000);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
You don't use document.write like that - it is supposed to be used as you build the page.

Doing it as you are doing it will clear everything on the page on the second loop
Does that mean document.write can't be used with setInterval and setTimeout?
Correct - that's not how document.write works

edit
I suppose you could document.write your two functions again but why would you do it that way...???
0
Dave BaldwinFixer of ProblemsCommented:
Doing it as you are doing it will clear everything on the page on the second loop
That's exactly what I saw happening.  Which would also explain why it stopped after the second loop.
0
MBaronganAuthor Commented:
Using the <div> tag made it work right. Thank you for clarifying the correct usage of document.write().
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.