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 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    JS Countdown Refresh?

    What can I add to my code to refresh the timer's output?

    here's my Script:
    Code:
    <script language="javascript" type="text/javascript">
    
    today  = new Date();
    todayEpoch  = today.getTime();
    
    target = new Date("03 May, 2012"); 
    targetEpoch = target.getTime();
    
    daysLeft = Math.floor(((targetEpoch - todayEpoch) / (60*60*24)) / 1000);
    hoursLeft = Math.floor(((targetEpoch - todayEpoch) / (60*60)) /1000);
    minutesLeft = Math.floor(((targetEpoch - todayEpoch) / (60)) /1000);
    secondsLeft = Math.floor(((targetEpoch - todayEpoch)) /1000);
    
    hours = Math.floor((hoursLeft)-((daysLeft)*24)); 
    minutes = Math.floor((minutesLeft)-(hoursLeft*60));
    seconds = Math.floor((secondsLeft)-(minutesLeft*60));
    </script>
    and how it would be placed in a html document (snippet):
    Code:
    I  am 18 in: <br>
    <script>document.write(daysLeft);</script> Days, <br>
    <script>document.write(hours);</script> Hours, <br>
    <script>document.write(minutes);</script> Minutes and <br>
    <script>document.write(seconds);</script> seconds! <br>
    Any help appreciated!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    To have the countodwn update within the page you need to throw away all that antiquated JavaScript and start again.

    document.write can only run before the page loads and is only useful where you don't have access to generate the output on the server.

    See http://javascript.about.com/library/blcount1.htm for the sort of code you need to be able to have the countdown timer actually count down.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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