Web Analytics Made Easy -
StatCounter Problem with setTimeout() makes everything else dissapear - CodingForum

Announcement

Collapse
No announcement yet.

Problem with setTimeout() makes everything else dissapear

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      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?

      Comment


      • #4
        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; Aug 26, 2011, 11:22 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.

        Comment


        • #5
          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.
          Be yourself. No one else is as qualified.

          Comment


          • #6
            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

            Comment


            • #7
              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).
              Be yourself. No one else is as qualified.

              Comment


              • #8
                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; Aug 27, 2011, 03: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.

                Comment


                • #9
                  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; Aug 27, 2011, 03:39 AM.
                  Be yourself. No one else is as qualified.

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      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.

                      Comment


                      • #12
                        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?)
                        Be yourself. No one else is as qualified.

                        Comment


                        • #13
                          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.

                          Comment


                          • #14
                            Love it! My new signature!
                            Be yourself. No one else is as qualified.

                            Comment


                            • #15
                              Actually, thanks to Oscar Wilde.

                              Do you think that we are chronologically challenged?
                              Last edited by Philip M; Aug 28, 2011, 02: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.

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎