Javascript返回日期为()的递归函数的未定义值
我对递归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 = [
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);
这是日志:
有人能帮我解决这个问题吗?这是因为你在局部范围内声明了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--…抱歉,太多了