Reputation: 8717
I'm just working on this simple clock as a learning project
<html>
<head>
<script type="text/javascript">
window.onload = function()
{
var da = document.getElementById("display_area");
function setClockDisplay()
{
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var currentTime = hour + ":" + minute + ":" + second;
da.innerHTML = currentTime;
}
setInterval(setClockDisplay(), 1000);
}
</script>
</head>
<body>
<p id="display_area"></p>
</body>
</html>
when I go to the page it displays the timestamp from when the page is originally loaded, but does not update every second like I would think. What am I missing?
Upvotes: 0
Views: 1398
Reputation: 126042
Use:
setInterval(setClockDisplay, 1000);
instead. setInterval
expects a function reference (you were invoking the function before).
Updated code: http://jsfiddle.net/VvG3k/
Upvotes: 7