0
<html>

<body>

<script type="text/javascript" language="javascript">
function renderTime () {

var currentTime = new Date();
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();

if (h < 10) 
{ h = "0" + h;
}


if (m < 10) 
{ m = "0" + m;
}


if (s < 10) 
{ s = "0" + s;
}

myClock.textContent = h + ":" + m + ":" + s;
myClock.intterText = h + ":" + m + ":" + s;


setTimeout ('renderTime()',1000);


}
renderTime();
</script>

</body>
</html>

THIS is my code and i am trying to get a current running digital 24 hour clock running, i have followed code and searched every where but my code isn't working? What am i doing wrong?

2 Answers2

0

Your code has several issues. JavaScript won't correct spelling errors for you! As noted above, you need to use innerText, not intterText.

Your renderTime() function was opened, but not closed, which will throw an "Unexpected end of input" error (check your developer console).

Also: you'll want to use setInterval(), not setTimeout(). setInterval() will run a function in intervals based on a specified time, set in milliseconds. 1000 milliseconds = 1 second. So, that looks like this:

setInterval(function(){
    // Code to run ...
}, 1000);

Here's a working JSFiddle:
http://jsfiddle.net/zc44tuea/

printr
  • 157
  • 8
0

There are a couple problems with your code, as people have mentioned in the comments. First of all, you misspelled innerText. Second, myclock is undefined. I assume you mean for it to be a <p> tag. As @printr mentioned, you should use setInterval. But here is working code, with minimal modifications from yours:

<html>
    <body>
        <p id="clock"></p>
    <script type="text/javascript" language="javascript">
        var myClock = document.getElementById("clock");
        function renderTime () {
            var currentTime = new Date();
            var h = currentTime.getHours();
            var m = currentTime.getMinutes();
            var s = currentTime.getSeconds();

            if (h < 10) {
                h = "0" + h;
            }


            if (m < 10) {
                m = "0" + m;
            }


            if (s < 10) {
                s = "0" + s;
            }

            //myClock.textContent = h + ":" + m + ":" + s;
            myClock.innerText = h + ":" + m + ":" + s;

            setTimeout(renderTime, 1000);
        }
        renderTime();
    </script>
</body>

c0d3rman
  • 652
  • 6
  • 14