-
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>
-
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>
-
Still not working. I've tested in IE6 and Firefox 0.8. It displays it the one time, then just sits there.
-
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>
-
Ok thanks, that makes sense :)