CommandoMan CommandoMan - 5 months ago 15
Javascript Question

SetInterval Javascript not working- showing blank

I want to create a clock that will update itself every second.
For some reason when I try to run the code I get a blank page.

this is the Javascript part:

function test()
{
var today = new Date();

var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();

var hr = today.getHours();
var mins = today.getMinutes();
var sec = today.getSeconds();

document.body.innerHTML = day + "/" + month + "/" + year + " " + hr + ":" + mins + ":" + secs;
}

setInterval(test, 1000);


this is the HTML part:

<html lang="en">

<head>
</head>

<body name="body">
<script type="text/javascript" src="JS-test.js"></script>

</body>

</html>


I've tried a few other variations and none seam to work.
How do I fix it to actually display something?
thanks in advance.
(I am a newbie in javascript)

Answer

Change secs to sec

 var sec = today.getSeconds();

    document.body.innerHTML = day + "/" + month + "/" + year + "   " + hr + ":" + mins + ":" + sec;