Brodie
Brodie

Reputation: 8717

javascript setInterval() not firing on timer

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

Answers (1)

Andrew Whitaker
Andrew Whitaker

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

Related Questions