简体   繁体   中英

Values returning as NaN

I was just finishing and testing my countdown function for JavaScript, and my variables are returning as NaN. It's longer than the scripts that I usually write, so I may not be able to find stupid mistakes.

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!");
function (second, miniute, hour, day, month, year)
...
countdown(0, 0, 25, 12, 2013);

You only passed in 5 parameters, so year is undefined, which when converted to a number gives NaN .

Your code seems immensely complicated... Here, have my countdown script :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)));

I found some problem... than i rebuild your code, see comment above:

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

:) i not test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM