javascripttime

Javascript clock update on the minute help?


ok i have a cool digital clock and javascript but i cant get it to update every minute, help?

<html>
<head>
<style type="text/css">
@font-face {
    font-family: Digital;
    src: url('digital.ttf');
}
html {
    font-family: "Digital";
    font-size: 130px;
}

#clock {
    color: red;
}
</style>
<script type="text/javascript" language="JavaScript">
function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (minute < 10) { minute = "0" + minute; }
   var timeString = hour +
                    ':' +
                    minute
   return timeString;
}
</script>
</head>
<body bgcolor="#000;" onload="init();">
<p id="clock" align="center">hi</p>
<script type="text/javascript" language="JavaScript"><!--
var clockTime = getClockTime();
document.getElementById('clock').innerHTML = clockTime;
</script>
<script type=text/javascript>

function init()
{
    getClockTime();
    window.setInterval(getClockTime,30000);
}

</script>
</body>
</html>

Solution

  • <html>
    <head>
    <style type="text/css">
    @font-face {
        font-family: Digital;
        src: url('digital.ttf');
    }
    html {
        font-family: "Digital";
        font-size: 130px;
    }
    
    #clock {
        color: red;
    }
    </style>
    <script type="text/javascript" language="JavaScript">
    function getClockTime()
    {
       var now    = new Date();
       var hour   = now.getHours();
       var minute = now.getMinutes();
       var second = now.getSeconds();
       if (hour   > 12) { hour = hour - 12;      }
       if (hour   == 0) { hour = 12;             }
       if (minute < 10) { minute = "0" + minute; }
       var timeString = hour +
                        ':' +
                        minute + 
                        ':' +
                        second
       document.getElementById('clock').innerHTML = timeString;
    }
    </script>
    </head>
    <body bgcolor="#000;" onload="init();">
    <p id="clock" align="center">hi</p>
    <script type="text/javascript" language="JavaScript"><!--
    var clockTime = getClockTime();
    
    </script>
    <script type=text/javascript>
    
    function init()
    {
        getClockTime();
        setInterval(getClockTime,1000);
    }
    
    </script>
    </body>
    </html>