2016-11-15 32 views
2

我有一個遞歸的JavaScript函數問題未定義的遞歸函數 - 返回值是不確定的:的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); 

這是日誌:

  1. 如果( )聲明SaturdayFunc.Sat Dec 23 2017 11:11:53 GMT + 0100 if() Statement Sunday Func.Sun Dec 24 2017 11:11:53 GMT + 0100 if() Statement Holiday Func.Mon Dec 25 2017 11: 11:53 GMT + 0100 if() Statement Holiday Func.Tue Dec 26 2017 11:11:53 GMT + 0100 else 語句 - 值:undefined Func。 - Return:undefined

有人可以幫我解決這個問題嗎?

+0

你'next'是不確定的,因爲你設置功能下一個變(局部範圍)。每次你的函數返回遞歸(在這種情況下假期)它設置本地'下一個'變量。然後finaly(得到正確的日期),這是沒有'下一個'變量定義 - undefined - ...對不起嘴巴 – RizkiDPrast

回答

0

這是因爲您在本地範圍內聲明瞭var,並且在遞歸fn觸發時您不返回任何內容。我想你想要next從遞歸fn保存以前的日期。請檢查這一個:

//next will return undefined if no recursive function called 
 
var next; 
 
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); 
 
      let nextday = new Date(timesec.getTime() + 24 * 60 * 60 * 1000); 
 
      next = timesec; 
 
      return get_next_weekday(nextday); 
 

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

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

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

 
var daydeliver = get_next_weekday(new Date(2016, 11, 11)); 
 
console.log("Func. - Return: " + daydeliver);
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>

+0

謝謝你,你是我的英雄!我必須學習很多東西。 – Tobschmi

+0

我很高興它可以幫助。讓我們學習很多:) – RizkiDPrast