Need help with simple Javascript Timer

Discussion in 'JavaScript' started by RogerDodgr, May 14, 2010.

  1. #1
    I want this to start running as soon as this page is loaded. It is for a Drupal site (a CMS), so I do not want to add a 'onload' to the body tag. I also don't want to alter the header text (which will permanently load to every page from now on). I just want to do this locally within the HTML.



    This doesn't work; I think it is obvious what I am trying to do; it's just a timer:
    
    <h1 id="header">Old Header</h1>
    
    <script type="text/javascript">
      document.getElementById("header").innerHTML=0;
      var t;
      var c = 1;
      while(true){
         document.getElementById('header').inerHTML=c;
         c=c+1;
         timedCount(1000);
                                        //t=setTimeout("timedCount()",1000);   <--if I replace the above line with this; it does not work either.
    }  
    </script>
    
    Code (markup):
     
    RogerDodgr, May 14, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    document.getElementById('header').inerHTML=c; <---
     
    gapz101, May 15, 2010 IP
  3. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this,

    <h1 id="header">Old Header</h1>

    <script type="text/javascript">
      document.getElementById("header").innerHTML=0;
      var t;
      var c = 1;
      
      function timedCount() {
     
         document.getElementById('header').innerHTML=c;
         c=c+1;
         t=setTimeout(timedCount,1000);
      }
    
      timedCount(); 
    </script>
    Code (markup):
     
    unigogo, May 21, 2010 IP