Javascript时钟没有显示出来

时间:2015-08-31 16:10:36

标签: javascript clock

所以我有一个HTML网站(实际上并不在线),而我只是搞乱它。最近我添加了一个时钟,然而,在用它进行大量编辑之后,似乎有一个我找不到的错误。这是代码:

<!DOCTYPE html>
<html>
<script type="text/javascript">
<!--
function updateTime() { var monthNames = ["January", "February", "March", "April", "May", "June
function updateTime() {
var currentTime = new Date();
var dayWk = currentTime.getDay();
var year = currentTime.getYear() - 100 + 2000;
var month = monthNames[currentTime.getMonth()];
var date = currentTime.getDate();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var dayWkNm = " ";
var dateFuncVar = date;
var dateSuffix = " ";
if (minutes < 10){
    minutes = "0" + minutes;
}
if (seconds < 10){
    seconds = "0" + seconds;
}
if (dayWk == 0){
    dayWkNm = "Sunday";
}
if (dayWk == 1){
    dayWkNm = "Monday";
}
if (dayWk == 2){
    dayWkNm = "Tuesday";
}
if (dayWk == 3){
    dayWkNm = "Wednesday";
}
if (dayWk == 4){
    dayWkNm = "Thursday";
}
if (dayWk == 5){
    dayWkNm = "Friday";
}
if (dayWk == 6){
    dayWkNm = "Saturday";
}
function dateFunc(dateFuncVar) {
    while (dateFuncVar > 9) {
        dateFuncVar = dateFuncVar - 10;
    }

    if (dateFuncVar == 1){
        dateSuffix = "st";
    } else if (dateFuncVar == 2){
        dateSuffix = "nd";
    } else if (dateFuncVar == 3){
        dateSuffix = "rd";
    } else {
        dateSuffix = "th";
    }
    return dateFuncVar;
}
var text = dayWkNm + "the" + date + " of " + month + ", " + year + " " + hours + ":" + minutes + ":" + seconds;
if(hours > 11){
    text+=" PM";
    hours = hours - 12;
} else {
    text+=" AM";
}

document.getElementById("time").innerHTML=text;
}

setInterval(updateTime, 1000);
//-->
</script>
<head>
<title>
Welcome to the Goto home page
</title>
<h3>
<p style="text-align:right" id="time">
</p>
</h3>
</head>

2 个答案:

答案 0 :(得分:0)

看看你返回“dateFuncVar”的位置。我愿意打赌你的其余代码没有被执行。

答案 1 :(得分:0)

http://plnkr.co/edit/PtlbPQQHT3wIzDIafaN7?p=preview

有一些错误:

  1. 应该是&#34;否则如果&#34;不是&#34; elseif&#34;
  2. 您过早地返回dateFuncVar
  3. getMonthName不是一个函数,所以我用Get month name from Date的答案替换它(如果你接受这个答案,那么也要回答那个问题和答案)
  4. 不是错误,但我会使用setInterval而不是在函数内重做setTimeout。

    function updateTime(){           var monthNames = [&#34; January&#34;,&#34; February&#34;,&#34; March&#34;,&#34; April&#34;,&#34; May&#34;,& #34; 6月&#34 ;,           &#34; 7月&#34;,&#34; 8月&#34;,&#34; 9月&#34;,&#34; 10月&#34;,&#34; 11月&#34;,&#34; 12月& #34;         ];

        var currentTime = new Date();
        var dayWk = currentTime.getDay();
        var year = currentTime.getYear() - 100 + 2000;
        var month = monthNames[currentTime.getMonth()];
        var date = currentTime.getDate();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        var dayWkNm = " ";
        var dateFuncVar = date;
        var dateSuffix = " ";
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        if (seconds < 10){
            seconds = "0" + seconds;
        }
        if (dayWk == 0){
            dayWkNm = "Sunday";
        }
        if (dayWk == 1){
            dayWkNm = "Monday";
        }
        if (dayWk == 2){
            dayWkNm = "Tuesday";
        }
        if (dayWk == 3){
            dayWkNm = "Wednesday";
        }
        if (dayWk == 4){
            dayWkNm = "Thursday";
        }
        if (dayWk == 5){
            dayWkNm = "Friday";
        }
        if (dayWk == 6){
            dayWkNm = "Saturday";
        }
        function dateFunc(dateFuncVar) {
            while (dateFuncVar > 9) {
                dateFuncVar = dateFuncVar - 10;
            }
    
            if (dateFuncVar == 1){
                dateSuffix = "st";
            } else if (dateFuncVar == 2){
                dateSuffix = "nd";
            } else if (dateFuncVar == 3){
                dateSuffix = "rd";
            } else {
                dateSuffix = "th";
            }
            return dateFuncVar;
        }
        var text = dayWkNm + "the" + date + " of " + month + ", " + year + " " + hours + ":" + minutes + ":" + seconds;
        if(hours > 11){
            text+=" PM";
            hours = hours - 12;
        } else {
            text+=" AM";
        }
    
        document.getElementById("time").innerHTML=text;
    }
    
    setInterval(updateTime, 1000);