Javascript 时钟未显示

Javascript Clock not showing up

所以我有一个 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>

看看你要返回的地方"dateFuncVar"。我敢打赌您的其余代码不会被执行。

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

有一些错误:

  1. 应该是"else if"不是"elseif"
  2. 您返回 dateFuncVar 的时间太早了
  3. getMonthName 不是一个函数,所以我用 Get month name from Date 的答案替换了它(如果你接受这个答案,也请投票给那个问题和答案)
  4. 不是错误,但我会使用 setInterval 而不是在函数内部重做 setTimeout。

    函数更新时间(){ var monthNames = ["January", "February", "March", "April", "May", "June", "July"、"August"、"September"、"October"、"November"、"December" ];

        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);