以下 JavaScript 在 Firefox 中不起作用,我能得到的唯一调试错误是“[06:29:53.518] SyntaxError: 09 is not a legal ECMA-262 octal constant”,它与 var month = '.date(' m', $time).'; 线。
此代码在 Internet Explorer 和 Chrome 中完美运行,仅在 Firefox 中它不起作用,尽管使用警告框突出显示它通常会停止的行没有给出有关问题的提示。
var hours = '.date('G', $time).';
var minutes = '.date('i', $time).';
var seconds = '.date('s', $time).';
var day = '.date('d', $time).';
var month = '.date('m', $time).';
var year = '.date('o', $time).';
function getDayCount(monthIndex, yearIndex)
{
switch ( monthIndex )
{
case 0: return 31;
case 1: return isLeapYear(yearIndex) ? 29 : 28;
case 2: return 31;
case 3: return 30;
case 4: return 31;
case 5: return 30;
case 6: return 31;
case 7: return 31;
case 8: return 30;
case 9: return 31;
case 10: return 30;
case 11: return 31;
}
return 31;
}
function isLeapYear(yearIndex)
{
return new Date(yearIndex, 2-1, 29).getDate() == 29;
}
function getDayName(year, month, day, hours, minutes, seconds)
{
var d = new Date(year, month, day, hours, minutes, seconds, 0);
var weekday= new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
return weekday[d.getDay()];
}
function updateClock()
{
window.seconds++;
if ( window.seconds == 60 ) {
window.minutes++;
window.seconds = 0;
}
if ( window.minutes == 60 ) {
window.hours++;
window.minutes = 0;
}
if ( window.hours == 24 ) {
window.day++;
window.hours = 0;
}
if ( (window.day - 1) == getDayCount(window.month - 1, window.year) ) {
window.month++;
window.day = 1;
}
if ( window.month == 12 ) {
window.year++;
window.month = 1;
}
var hourString = window.hours.toString();
var minuteString = window.minutes.toString();
var secondsString = window.seconds.toString();
var dayString = window.day.toString();
var monthString = window.month.toString();
var yearString = window.year.toString();
if ( minuteString.length < 2 )
minuteString = "0".concat(minuteString);
if ( hourString.length < 2 )
hourString = "0".concat(hourString);
if ( secondsString.length < 2 )
secondsString = "0".concat(secondsString);
if ( dayString.length < 2 )
dayString = "0".concat(dayString);
if ( monthString.length < 2 )
monthString = "0".concat(monthString);
document.getElementById("hour").innerText = hourString;
document.getElementById("minute").innerText = minuteString;
document.getElementById("second").innerText = secondsString;
document.getElementById("day").innerText = dayString;
document.getElementById("month").innerText = monthString;
document.getElementById("year").innerText = yearString;
document.getElementById("dayName").innerText = getDayName(year, month - 1, day, hours, minutes, seconds);
}
function init()
{
window.setInterval(function(){updateClock()}, 1000);
}
相关的 HTML:
<h2>Live Chat - <span id="hour"></span>:<span id="minute"></span>:<span id="second"></span> <span id="dayName"></span> <span id="day"></span>/<span id="month"></span>/<span id="year"></span></h2>
正文初始化:
<body onload="javascript:init();">