Javascript Interval不重复

时间:2014-06-01 09:03:54

标签: javascript html intervals

我的HTML / Javascript文档中有一个setInterval。 现在,据我所知,setInterval部分的输出与我的另一个文档完全相同,并且在那里工作得很好。但在此期间,间隔不是每1秒重复一次。它运行一次,然后停止。我缺少任何明显的错误吗?

<html>
<head>
<title>Time</title>
</head>
<body>

<p id="line-one" />
<p id="line-two" />
<p id="line-three" />
<p id="line-four" />
<p id="seconds" />    

<SCRIPT type='text/JavaScript'language='JavaScript'>

var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var nIntervId;

function counter()
{   
    if (typeof(nIntervId) != "undefined") {
        clearInterval(nIntervId);
    }

    nIntervId = setInterval(changeASCII, 1000);
}

function changeASCII()
{
    document.getElementById("seconds").innerHTML = second;
    switch(second) {
        case 00:
        document.getElementById("line-one").innerHTML = " ___...___ ";
        document.getElementById("line-two").innerHTML = "|...|.|...|";
        document.getElementById("line-three").innerHTML = "|.|.|.|.|.|";
        document.getElementById("line-four").innerHTML = "|___|.|___|";
        break;
        case 01:
        document.getElementById("line-one").innerHTML = " ___   ___";
        document.getElementById("line-two").innerHTML = "|   | |_  |";
        document.getElementById("line-three").innerHTML = "| | |  _| |_";
        document.getElementById("line-four").innerHTML = "|___| |_____|";
        break;
        default:
        document.getElementById("line-one").innerHTML = "Nothing";
        document.getElementById("line-two").innerHTML = "To";
        document.getElementById("line-three").innerHTML = "See";
        document.getElementById("line-four").innerHTML = "Here";
        break;
    }
}


window.onload = counter();

</SCRIPT>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

一切都很好,但是,你没有更新第二个的实际值因此你从日期得到它,也没有更新。

在小提琴中检查出来;

http://jsfiddle.net/H8C4W/

function changeASCII()
{
    now=new Date();
    second=now.getSeconds();
...