Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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>
    Last edited by Philip M; 08-26-2011 at 05:22 PM.

    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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.
    Last edited by Philip M; 08-27-2011 at 09: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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.
    Last edited by Old Pedant; 08-27-2011 at 09:39 AM.
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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.

  • #11
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    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.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Actually, thanks to Oscar Wilde.

    Do you think that we are chronologically challenged?
    Last edited by Philip M; 08-28-2011 at 08:58 PM.

    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •