我刚刚完成并测试了我的JavaScript倒计时功能,我的变量以NaN的形式返回。它比我通常写的脚本更长,所以我可能无法找到愚蠢的错误。
var time = "";
var countdown = function (second, miniute, hour, day, month, year) {
var x = new Date();
var today = new Date();
var s = second - 1;
var m = miniute - 1;
var h = hour - 1;
var d = day - 1;
var mo = month - 1;
var y = year - 1;
var monthdays;
var daysInYear;
var leap;
var leapyear = y % 4;
x.setFullYear(y, mo, d, h, m, s);
if (m == 4) {
monthdays = 30;
}
if (m == 6) {
monthdays = 30;
}
if (m == 9) {
monthdays = 30;
}
if (m == 11) {
monthdays = 30;
} else if (m == 2) {
if (leapyear === 0) {
monthdays = 29;
leap = true;
} else {
monthdays = 28;
}
} else {
monthdays = 31;
}
if (leap === true) {
daysInYear = 366;
} else {
daysInYear = 366;
}
var seconds = Math.floor((x.getTime() - today.getTime()) / (1000));
var minutes = Math.floor((x.getTime() - today.getTime()) / (1000 * 60));
var hours = Math.floor((x.getTime() - today.getTime()) / (1000 * 60 * 60));
var days = Math.floor((x.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
var months = Math.floor((x.getTime() - today.getTime()) / (1000 * 60 * 60 * 24 * monthdays));
var years = Math.floor((x.getTime() - today.getTime()) / (1000 * 60 * 60 * 24 * monthdays * daysInYear));
time = years + " Year(s), " + months + " Month(s), " + days + " Day(s), " + hours + " Hour(s), " + minutes + " Minute(s), and " + seconds + " Second(s). ";
};
countdown(0, 0, 25, 12, 2013);
alert(time + "'til christmas!");
答案 0 :(得分:3)
function (second, miniute, hour, day, month, year)
...
countdown(0, 0, 25, 12, 2013);
您只传入了5个参数,因此year
未定义,转换为数字时会NaN
。
答案 1 :(得分:0)
你的代码看起来非常复杂......在这里,有我的倒计时脚本:p
function getTimeTo(targetDate) {
var delta = new Date().getTime() - targetDate.getTime(),
seconds = delta % 60,
minutes = Math.floor(delta/60) % 60,
hours = Math.floor(delta/3600) % 24,
days = Math.floor(delta/3600/24) % 7,
weeks = Math.floor(delta/3600/24/7);
// note that beyond weeks it is very difficult to calculate time.
// however, if you're counting to Christmas, that's fine because
// there is less than one month left!
return (
(weeks ?weeks +" week" +(weeks ==1?"":"s")+" " : "")
+(days ?days +" day" +(days ==1?"":"s")+" " : "")
+(hours ?hours +" hour" +(hours ==1?"":"s")+" " : "")
+(minutes?minutes+" minutes"+(minutes==1?"":"s")+" " : "")
+(seconds?seconds+" seconds"+(seconds==1?"":"s") : "")
);
}
alert(getTimeTo(new Date(2013, 12-1 /*month*/, 25, 0, 0, 0)));
答案 2 :(得分:0)
我发现了一些问题...而不是重建代码,请参阅上面的评论:
var time = "";
var countdown = function (s, m, h, d, month, y) { //use short version of your var
var today = new Date();
var mo = month - 1; //is the only var ZERO BASED.
var monthdays;
var daysInYear;
var leap;
var leapyear = y % 4;
var x = new Date(y, mo, d, h, m, s);
if (m == 4 || m == 6 || m == 9 || m == 11) { //contract the syntax
monthdays = 30;
} else if (m == 2) {
if (leapyear === 0) {
monthdays = 29;
leap = true;
} else {
monthdays = 28;
}
} else {
monthdays = 31;
}
if (leap === true) {
daysInYear = 366;
} else {
daysInYear = 365; //if leap false years are 356... true?
}
//I change this part of your code:
var gap = x.getTime() - today.getTime();
var years = parseInt(gap / (1000 * 60 * 60 * 24 * monthdays * daysInYear));
gap = gap % (1000 * 60 * 60 * 24 * monthdays * daysInYear);
var months = parseInt(gap / (1000 * 60 * 60 * 24 * monthdays));
gap = gap % (1000 * 60 * 60 * 24 * monthdays);
var days = parseInt(gap / (1000 * 60 * 60 * 24));
gap = gap % (1000 * 60 * 60 * 24);
var hours = parseInt(gap / (1000 * 60 * 60));
gap = gap % (1000 * 60 * 60);
var minutes = parseInt(gap / (1000 * 60));
gap = gap % (1000 * 60);
var seconds = parseInt(gap / 1000);
//edit end
time = years + " Year(s), " + months + " Month(s), " + days + " Day(s), " + hours + " Hour(s), " + minutes + " Minute(s), and " + seconds + " Second(s). ";
};
countdown(0, 0, 0, 25, 12, 2013); // remember all attribute.
alert(time + "'til christmas!");
:)我没考试。