Results 1 to 5 of 5

Thread: Dynamic clock?

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Dynamic clock?

    I am trying to make a clock that ticks in the status bar. It displays the correct time, but it doesn't update every second. What is wrong with the code?

    PHP Code:
    <html>
    <
    head>
    <
    title>Clock</title>
    <
    script>
    var 
    today = new Date();
    var 
    hours;
    var 
    minutes;
    var 
    seconds;
    var 
    ampm;
    var 
    total;

    function 
    updateTimer()
    {
        
    hours today.getHours();
        
    minutes today.getMinutes();
        
    seconds today.getSeconds();
        
    ampm "AM";

        if (
    hours >= 12)
            
    ampm "PM";

        if (
    hours >= 12)
            
    hours -= 12;

        if (
    minutes 10)
            
    minutes "0" minutes;

        if (
    seconds 10)
            
    seconds "0" seconds;

        
    total hours ":" minutes ":" seconds " " ampm;
        
    window.status total;

        
    setTimeout("updateTimer()"1000);
    }

    </
    script>
    </
    head>
    <
    body onLoad="updateTimer()">
    </
    body>
    </
    html
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Code:
    <html>
    <head>
    <title>Clock</title>
    <script>
    var today = new Date();
    var hours;
    var minutes;
    var seconds;
    var ampm;
    var total;
    
    function updateTimer()
    {
        hours = today.getHours();
        minutes = today.getMinutes();
        seconds = today.getSeconds();
        ampm = "AM";
    
        if (hours >= 12)
        {
        ampm = "PM";
        hours -= 12;
        }
    
        if (minutes < 10)
        minutes = "0" + minutes;
    
        if (seconds < 10)
        seconds = "0" + seconds;
    
        total = hours + ":" + minutes + ":" + seconds + " " + ampm;
        window.status = total;
    }
    setInterval("updateTimer()",1000)
    </script>
    </head>
    <body>
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Still not working. I've tested in IE6 and Firefox 0.8. It displays it the one time, then just sits there.
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    now it works

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Dynamic Clock</title>
    <script type="text/javascript">
    function updateTimer()
    {
    	var today = new Date();
    	var hours;
    	var minutes;
    	var seconds;
    	var ampm;
    	var total;
    
        hours = today.getHours();
        minutes = today.getMinutes();
        seconds = today.getSeconds();
        ampm = "AM";
    
        if (hours >= 12)
        {
        ampm = "PM";
        hours -= 12;
        }
    
        if (minutes < 10)
        minutes = "0" + minutes;
    
        if (seconds < 10)
        seconds = "0" + seconds;
    
        total = hours + ":" + minutes + ":" + seconds + " " + ampm;
        window.status = total;
    }
    </script>
    </head>
    <body onLoad="setInterval('updateTimer()','1000')">
    <p>It wasn't working because Date() has to be included with the function so it is also updates constantly. Otherwise you are only using
    the date (the time) from when the page loaded over and over again.</p>
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  5. #5

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok thanks, that makes sense
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

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



Click Here to Expand Forum to Full Width