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.
Results 1 to 3 of 3

Thread: countdown timer

  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts

    countdown timer

    hi guys,

    i know you'll prob tell me to google or some other thing but i want to know how to have my javascript countdown in real time.....

    Code:
    <script>
        <!--
        /*
        iTsOnLyBaRnEy's Countdown Timer
        http://itsonlybarney.x10hosting.com/
        itsonlybarney [at] gmail [dot] com
        */
        var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        function countdown(yr,m,d) {
          var today = new Date();
          year = yr;
          month = m;
          day = d;
          var now_year = today.getYear();
          var now_month = today.getMonth();
          var now_day = today.getDate();
          var now_hour = today.getHours();
          var now_min = today.getMinutes();
          var now_sec = today.getSeconds();
          
          var now = months[now_month]+ " " +now_day+ ", " +now_year+ " " +now_hour+ ":" +now_min+ ":" +now_sec ;
          var then = months[m-1]+ " " +d+ ", " +yr ;
          
          difference = Date.parse(then) - Date.parse(now);
          
          diff_day = Math.floor(difference/(60*60*1000*24)*1);
          diff_hour = Math.floor((difference%(60*60*1000*24))/(60*60*1000)*1);
          diff_min = Math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
          diff_sec = Math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
          
          document.write("Only "+diff_day+ " days, " +diff_hour+ " hours, " +diff_min+ " minutes, and " +diff_sec+ " seconds until Christmas!");
          setTimeout("countdown(year,month,day)",1000)
        }
    
        countdown(2005,12,25);
        -->
    </script>
    but the seconds stay a constant until i refresh the page....is there a way to do it without refreshing the page....

    i think i saw it done with a textbox but i don't really want to put a text box on my page....

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    943
    Thanks
    0
    Thanked 129 Times in 128 Posts
    try this:
    Code:
    <script>
        <!--
        /*
        iTsOnLyBaRnEy's Countdown Timer
        http://itsonlybarney.x10hosting.com/
        itsonlybarney [at] gmail [dot] com
        */
        var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        function countdown(yr,m,d) {
          var today = new Date();
          year = yr;
          if (!document.all){
          year=year%1900
          }
          month = m;
          day = d;
          var now_weekday = today.getDay();
          var now_year = today.getYear();
          var now_month = today.getMonth();
          var now_day = today.getDate();
          var now_hour = today.getHours();
          var now_min = today.getMinutes();
          var now_sec = today.getSeconds();
          
          var now = months[now_month]+ " " +now_day+ " " +now_year+ " " +now_hour+ ":" +now_min+ ":" +now_sec ;
          var then = months[m-1]+ " " +d+ ", " +year+" 18:00:00";
          
          difference = Date.parse(then) - Date.parse(now);
          
          diff_day = Math.floor(difference/(60*60*1000*24)*1);
          diff_hour = Math.floor((difference%(60*60*1000*24))/(60*60*1000)*1);
          diff_min = Math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
          diff_sec = Math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
    
          document.getElementById('ShowTime').innerHTML="Only "+diff_day+ " days, " +diff_hour+ " hours, " +diff_min+ " minutes, and " +diff_sec+ " seconds until Christmas!";
          setTimeout("countdown(year,month,day)",1000)
          }
    
       
    //   -->
    </script>
    <body onload="countdown(2005,12,26);">
    
    <span style="width:420px" id="ShowTime">Christmas Countdown</span>

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much....


  •  

    Posting Permissions

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