简体   繁体   English

使用javascript的数字当前运行时钟不起作用

[英]digital current running clock using javascript not working

<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? 这是我的代码,我正在尝试使当前的数字24小时时钟运行起来,我已跟踪代码并在每个地方搜索,但我的代码不起作用? What am i doing wrong? 我究竟做错了什么?

Your code has several issues. 您的代码有几个问题。 JavaScript won't correct spelling errors for you! JavaScript不会为您纠正拼写错误! As noted above, you need to use innerText , not intterText . 如上所述,您需要使用innerText ,而不是intterText

Your renderTime() function was opened, but not closed, which will throw an "Unexpected end of input" error (check your developer console). 您的renderTime()函数已打开,但未关闭,这将引发“输入意外结束”错误(请检查开发者控制台)。

Also: you'll want to use setInterval(), not setTimeout(). 另外:您将要使用setInterval()而不是setTimeout()。 setInterval() will run a function in intervals based on a specified time, set in milliseconds. setInterval()将基于指定的时间间隔(以毫秒为单位setInterval()运行函数。 1000 milliseconds = 1 second. 1000毫秒= 1秒。 So, that looks like this: 因此,看起来像这样:

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

Here's a working JSFiddle: 这是一个工作的JSFiddle:
http://jsfiddle.net/zc44tuea/ http://jsfiddle.net/zc44tuea/

There are a couple problems with your code, as people have mentioned in the comments. 正如人们在注释中提到的那样,您的代码有几个问题。 First of all, you misspelled innerText . 首先,您拼写了innerText Second, myclock is undefined. 其次, myclock是不确定的。 I assume you mean for it to be a <p> tag. 我假设您的意思是将其作为<p>标记。 As @printr mentioned, you should use setInterval . 如@printr所述,您应该使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM