所以我有一个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>
答案 0 :(得分:0)
看看你返回“dateFuncVar”的位置。我愿意打赌你的其余代码没有被执行。
答案 1 :(得分:0)
http://plnkr.co/edit/PtlbPQQHT3wIzDIafaN7?p=preview
有一些错误:
不是错误,但我会使用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);