Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-25-2011, 08:20 PM   PM User | #1
Belloshoes
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Belloshoes is an unknown quantity at this point
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.

[CODE]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script LANGUAGE='JavaScript' type='text/javascript' SRC='http://www.theFinancials.com/syndicated/Free/EXk_EconomicStats_US.js'></script>
</head>

<body>

<div id="ticker6">
<script LANGUAGE='JavaScript' type='text/javascript' SRC='http://www.theFinancials.com/syndicated/Free/EXk_Interest_TBills.js'></script>
<script LANGUAGE='JavaScript' type='text/javascript'>
document.writeln(EXk_Interest_TBills('0050201336','100%','C9C9C9','yes','6B6B6B','Arial',11,1,'37373 7'));
</script>
</div>

<div id="ticker8">


<script type="text/javascript">

setTimeout("ticker8()", 5000);

function ticker8()
{
<!--START theFinancials.com Content-->
<!--copyright theFinancials.com - All Rights Reserved-->
document.writeln(EXk_EconomicStats_US('0199604514','100%','C9C9C9','yes','6B6B6B','Arial',11,1,'3737 37'));
<!--END theFinancials.com Content-->
}
</script>
</div>

</body>
</html>

[CODE]

Would really appreciate some help with this one.

Many thanks in advance
Belloshoes is offline   Reply With Quote
Old 08-25-2011, 08:37 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can *NOT* use document.write or document.writeln *AFTER* a page has loaded. (That is, after the browser has finished rendering the page...after window.onload time, say.)

If you do so, you WIPE OUT the ENTIRE contents of the page, including even the JavaScript that did the document.write.

You need to find a completely different way to do this.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-26-2011, 01:47 PM   PM User | #3
Belloshoes
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Belloshoes is an unknown quantity at this point
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).

What do you think?
Belloshoes is offline   Reply With Quote
Old 08-26-2011, 03:29 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.

This may be getting closer:-

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type='text/javascript' SRC='http://www.theFinancials.com/syndicated/Free/EXk_EconomicStats_US.js'></script>
</head>

<body> 

<div id="Ticker6"> 
<script type='text/javascript' SRC='http://www.theFinancials.com/syndicated/Free/EXk_Interest_TBills.js'></script>
<script type='text/javascript'>
var k = (EXk_Interest_TBills('0050201336','100%','C9C9C9','yes','6B6B6B','Arial',11,1,'37373 7'));
document.write(k);
</script>
</div>

<div id="Ticker8" style="visibility:hidden">
<script type="text/javascript">
setTimeout("ticker8()", 10000);
function ticker8() {
document.getElementById("Ticker8").style.visibility="visible";
}
var k =(EXk_EconomicStats_US('0199604514','100%','C9C9C9','yes','6B6B6B','Arial',11,1,'3737 37'));
document.write(k);
</script>
</div>
<br>

REST OF THE PAGE HERE

</body>
</html>
__________________

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.

Last edited by Philip M; 08-26-2011 at 04:22 PM..
Philip M is offline   Reply With Quote
Old 08-26-2011, 09:06 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-26-2011, 10:37 PM   PM User | #6
Belloshoes
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Belloshoes is an unknown quantity at this point
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.

Many Thanks guys
Belloshoes is offline   Reply With Quote
Old 08-26-2011, 10:54 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-27-2011, 08:27 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
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.
__________________

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.

Last edited by Philip M; 08-27-2011 at 08:30 AM..
Philip M is offline   Reply With Quote
Old 08-27-2011, 08:36 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 08-27-2011 at 08:39 AM..
Old Pedant is offline   Reply With Quote
Old 08-27-2011, 08:53 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
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.
Philip M is offline   Reply With Quote
Old 08-27-2011, 09:02 AM   PM User | #11
Belloshoes
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Belloshoes is an unknown quantity at this point
Haha......arrrrrre you twwwoooo..... an item?.......seem to be having a bit of a domestic here.

Anyway yes I am very happy with it. Thank you very much guys.
@Philip M I've gone through the code and understood it now, so no explanation required.
Belloshoes is offline   Reply With Quote
Old 08-28-2011, 05:29 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh, Philip and I both go way back.

Oh, not together. It's just that we are both, shall we say, more advanced in years than many here?

(We also both happen to love model trains, though Philip is a master and I'm a neophyte.)

(Oh, and Philip is a physician whereas I'm just sick.)

(Did I mention he lives in the UK and I'm in the Seattle, USA, area?)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-28-2011, 08:09 AM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Oh, Philip and I both go way back.

Oh, not together. It's just that we are both, shall we say, more advanced in years than many here?
And sadly we are no longer young enough to know everything.
__________________

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.
Philip M is offline   Reply With Quote
Old 08-28-2011, 06:55 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Love it! My new signature!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-28-2011, 07:52 PM   PM User | #15
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Actually, thanks to Oscar Wilde.

Do you think that we are chronologically challenged?
__________________

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.

Last edited by Philip M; 08-28-2011 at 07:58 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:03 AM.


Advertisement
Log in to turn off these ads.