我不是所有經歷過的代碼和我卡在朝着下面的代碼的Javascript while循環無限循環
我的代碼塊底部的while循環應該得到的日期,檢查今天是否是我們沒有運送的那一天(星期六,星期日,節假日),如果是,則增加1天,直到找到第二天我們開放並寫入文檔爲止。
var target = new Date();
var targetDay = target.getDay();
var targetDate = target.getDate();
var targetMonth = target.getMonth();
function checkIfClosedOnTarget(targetDay,targetDate,targetMonth){
var areOpenOnTarget = true;
if(
targetDay == 0 ||
targetDay == 6 ||
(targetDate == 1 && targetMonth == 0) || // New Year's Day
(targetMonth == 4 && targetDate >= 25 && targetDay == 1) || // Memorial Day
(targetMonth == 6 && targetDate == 4) || //Independence Day
(targetMonth == 8 && targetDate <= 7 && targetDay == 1)|| //Labor Day
(targetMonth == 10 && targetDate <= 28 && targetDate >= 22 && targetDay == 4)|| // Thanksgiving Day
(targetMonth == 11 && targetDate == 25)
){
areOpenOnTarget = false;
}
if(areOpenOnTarget){
return true;
}else{
return false;
}
};
function addDaysUntilNextOpenDay() {
while(checkIfClosedOnTarget(targetDay,targetDate,targetMonth) == false){
target.setDate(target.getDate() + 1);
}
};
addDaysUntilNextOpenDay();
document.write("<p>Next shipment will ship out on " + target.getMonth() + " " + target.getDate + ", " + target.getYear) + " at 4:00pm Pacific Standard Time ";
你應該只返回邏輯的句子在你的'checkIfClosedOnTarget'功能「」如果'聲明:
所以你設置後的第二天,你可能要對其進行更新。其餘代碼是多餘的。 – 2013-04-09 21:07:33
我認爲你可以做很多清理你的代碼。例如,你有一個名爲'checkIfClosedOnTarget'的方法,這意味着如果關閉,它會返回'true',但如果打開則返回true。羅嗦。嘗試將其重命名爲「isOpen」或「isOpenOn」。那麼你真的只需要在你的大if語句中返回false,並且在它之外是真實的。根本不需要'areOpenOnTarget'變量。 – 2013-04-09 21:10:27
fyi,這不是遞歸.... – sweetamylase 2013-04-09 21:10:37