If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Problem with setTimeout() makes everything else dissapear

I have 2 news tickers that I want to show one after the other. They are both widgets (javascript) that I grabbed from the stated website. However to get one to run after the other I felt like I needed a setTimeout(). So I made one of them delayed (ticker8) but instead of JUST appearing after 5000 ms, it appears and EVERYTHING ELSE DISAPPEARS. I know that it's something silly I just can't figure out what it is. The HTML code is below.

Thanks for the reply Old Pedant. I really appreciate it. However, I have tried it out and used document.writeln in another code and everything is stable runs normally. Even if i take the code below and run both div id=6 and div id=8 without using the setTimeout function then they both run fine and don't erase anything (but obviously they both run at the same time which isn't what I want it to do that's why I used setTimeout).

I think Old Pedant is right. Using setTimeout() causes the page to reload after 5 seconds, destroying the content and the script. Without setTimeout() the page finishes loading properly and both the tickers are shown. You can easily test this by observation.

Another problem is that you are using the identical id/name ticker8 for the div and the Javascript function. Make the div id Ticker8.

Yes. *BY DEFINITION* the result of a setTimeout runs *after* the page is loaded. Even if you use a one millisecond timeout, the code targeted by the setTimeout won't run until after the page is fully loaded. Which means that any document.write you do as a result of a setTimeout is guaranteed to wipe out the page.

THANK YOU! THANK YOU! Thank you very much Philip M. And you Old Pedant. Both of you have been extremely helpful.

Philip M. If it's not too much trouble, could you please talk me through (in as much detail as you can) the changes you made and how and why it NOW works with your modification. I would really appreaciate that so that I can better myself, improve and apply it to more divs.

Philip's code is a bit of a cheat. I'm not 100% sure it really is doing what you want.

If that call to EXk_EconomicStats_US() is time-sensitive (that is, if it matters--to the second--when it is called) then it won't do what you want.

What he is really doing there is calling EXk_EconomicStats_US() for *BOTH* ticker6 and ticker8 WHEN THE PAGE IS LOADED. And then he just keeps ticker8 hidden until the 10 seconds have elapsed. So if was important that ticker8 not even be *initialized* until after the 10 seconds, that code won't work as you want it.

But if that's not important, then his code works fine. And could even be simplified a tiny bit (not enough to worry about).

Philip's code is a bit of a cheat. I'm not 100% sure it really is doing what you want.

Neither am I! That's why I said "This may be getting closer:-" However, Belloshoes seems to like it.

But why should it matter to the second when the ticker commences? They are just statistics with nothing "live" about them.

Don't see how the code can be simplified in any valuable way. If you mean
var k = (EXk_Interest_TBills('0050201336','100%','C9C9C9','yes','6B6B6B','Arial',11,1,'37373 7'));
document.write(k);
I always do it like that so that I can insert alert(k); to observe the stuff being loaded.

Last edited by Philip M; 08-27-2011 at 08:30 AM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

DOH on me. I didn't read carefully enough. Thought it was calling the same function, just with different first argument. Ignore my comment re simpler code.

As for the timing stuff: If the charts are static, then why not load AND SHOW them both as the page is loaded in the first place? The purposeful 10 second delay bothered me. But you are probably right; he probably just does it for visual effect.

As for the timing stuff: If the charts are static, then why not load AND SHOW them both as the page is loaded in the first place? The purposeful 10 second delay bothered me. But you are probably right; he probably just does it for visual effect.

Well, that is what he had to start with! I just about see the point of the visual effect as otherwise the user is trying to read two tickers at once. Sort of.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.