Javascript返回日期为()的递归函数的未定义值

Javascript返回日期为()的递归函数的未定义值,javascript,jquery,Javascript,Jquery,我对递归javascript函数有问题-返回值未定义: function get_next_weekday(timesec) { var nextdaytogo = timesec; var nextday; var hour = timesec.getHours(); initstunden = 13 - hour; initminuten = 59 - timesec.getMinutes(); var holidays = [

我对递归javascript函数有问题-返回值未定义:

function get_next_weekday(timesec) {

    var nextdaytogo = timesec;
    var nextday;
    var hour = timesec.getHours();
    initstunden = 13 - hour;
    initminuten = 59 - timesec.getMinutes();
    var holidays = [
        new Date(2016, 11, 25).toDateString(), new Date(2016, 11, 26).toDateString(), new Date(2017, 00, 01).toDateString(), new Date(2017, 00, 06).toDateString(), 
        new Date(2017, 03, 14).toDateString(), new Date(2017, 03, 17).toDateString(), new Date(2017, 04, 01).toDateString(), new Date(2017, 04, 25).toDateString(), 
        new Date(2017, 05, 05).toDateString(), new Date(2017, 05, 15).toDateString(), new Date(2017, 09, 03).toDateString(), new Date(2017, 10, 01).toDateString(), 
        new Date(2017, 11, 25).toDateString(), new Date(2017, 11, 26).toDateString(), new Date(2018, 00, 01).toDateString(), new Date(2018, 00, 06).toDateString(), 
        new Date(2018, 02, 30).toDateString(), new Date(2018, 03, 02).toDateString(), new Date(2018, 04, 01).toDateString(), new Date(2018, 04, 10).toDateString(), 
        new Date(2018, 04, 21).toDateString(), new Date(2018, 04, 31).toDateString(), new Date(2018, 09, 03).toDateString(), new Date(2018, 10, 01).toDateString(), 
        new Date(2018, 11, 25).toDateString(), new Date(2018, 11, 26).toDateString()
    ];


    if ( $.inArray(timesec.toDateString(), holidays) > -1 || timesec.getDay() == 0 || timesec.getDay() == 6) {
        console.log("if() Statement")
        //Holiday
        if ( $.inArray(timesec.toDateString(), holidays) > -1){
            console.log("Holiday Func." + timesec);
            var nextday = new Date(timesec.getTime() + 24 * 60 * 60 * 1000);
            var next = get_next_weekday(nextday);

        }
        //Sunday
        else if (timesec.getDay() == 0){
            console.log("Sunday Func." + timesec);
            var nextday = new Date(timesec.getTime() + 24 * 60 * 60 * 1000);
            var next = get_next_weekday(nextday);

        }
        //Saturday
        else if (timesec.getDay() == 6){
            console.log("Saturday Func." + timesec);
            var nextday = new Date(timesec.getTime() + 24 * 60 * 60 * 1000);
            var next = get_next_weekday(nextday);

        }
        else console.log("Und. Func.");


    }
    else{
        console.log("else Statement - Value: " + next);
        return timesec;
    }
}

var daydeliver = get_next_weekday(aktuell);
console.log("Func. - Return: " + daydeliver);
这是日志:

  • if()语句SaturdayFunc.Sat 2017年12月23日11:11:53 GMT+0100 if() 声明2017年12月24日星期日11:11:53 GMT+0100如果() 2017年12月25日星期一11:11:53 GMT+0100 if()的假期职能声明 声明假日职能2017年12月26日星期二11:11:53 GMT+0100其他 语句-值:未定义的Func。-返回:未定义

  • 有人能帮我解决这个问题吗?

    这是因为你在局部范围内声明了var+当递归fn触发时你什么也不返回。我想您希望
    next
    保留递归fn中的上一个日期。请回顾这一点:

    //如果没有调用递归函数,next将返回undefined
    var-next;
    函数get_next_weekday(timesec){
    var nextdaytogo=timesec;
    var下一天;
    var hour=timesec.getHours();
    初始时间=13小时;
    initminuten=59-timesec.getMinutes();
    假期=[
    新日期(2016年11月25日)。toDateString(),新日期(2016年11月26日)。toDateString(),新日期(2017年01月00日)。toDateString(),新日期(2017年06月00日)。toDateString(),
    新日期(2017年3月14日)。toDateString(),新日期(2017年3月17日)。toDateString(),新日期(2017年4月1日)。toDateString(),新日期(2017年4月25日)。toDateString(),
    新日期(2017年5月5日)。toDateString(),新日期(2017年5月15日)。toDateString(),新日期(2017年9月3日)。toDateString(),新日期(2017年10月1日)。toDateString(),
    新日期(2017年11月25日)。toDateString(),新日期(2017年11月26日)。toDateString(),新日期(2018年01月00日)。toDateString(),新日期(2018年06月00日)。toDateString(),
    新日期(2018,02,30)。toDateString(),新日期(2018,03,02)。toDateString(),新日期(2018,04,01)。toDateString(),新日期(2018,04,10)。toDateString(),
    新日期(2018,04,21)。toDateString(),新日期(2018,04,31)。toDateString(),新日期(2018,09,03)。toDateString(),新日期(2018,10,01)。toDateString(),
    新日期(2018年11月25日)。toDateString(),新日期(2018年11月26日)。toDateString()
    ];
    如果($.inArray(timesec.toDateString(),假日)>-1 | | timesec.getDay()==0 | | timesec.getDay()==6){
    log(“if()语句”)
    //假日
    if($.inArray(timesec.toDateString(),假日)>-1){
    console.log(“假日函数”+timesec);
    设nextday=新日期(timesec.getTime()+24*60*60*1000);
    下一个=timesec;
    在下一个工作日(下一个工作日)返回get_;
    }
    //星期天
    else if(timesec.getDay()==0){
    console.log(“Sunday Func.+timesec”);
    设nextday=新日期(timesec.getTime()+24*60*60*1000);
    下一个=timesec;
    在下一个工作日(下一个工作日)返回get_;
    }
    //星期六
    else if(timesec.getDay()==6){
    console.log(“星期六函数”+timesec);
    设nextday=新日期(timesec.getTime()+24*60*60*1000);
    下一个=timesec;
    在下一个工作日(下一个工作日)返回get_;
    }
    else{console.log(“Und.Func.”);
    next=“Und.Func.”返回;
    }
    }
    否则{
    log(“else语句-值:”+next);
    返回时间秒;
    }    
    }
    var daydeliver=获取下一个工作日(新日期(2016年11月11日));
    控制台日志(“函数返回:+daydeliver”)
    
    您的
    下一个
    未定义,因为您在函数(本地范围)下设置了下一个变量。每次函数返回递归(本例中为假日)时,它都会设置局部
    next
    变量。最后(得到正确的日期),它不是定义的下一个变量--undefined--…抱歉,太多了