我正在開發一個應用程序,提供有關當地餐館的信息。所有的餐廳都有營業時間。只有一些餐館有歡樂時光。我不希望兩次使用同一代碼塊,而是希望能夠重用我的功能,以確定今天的開放時間,當前關閉時間或關閉時間,以確定歡樂時間和營業時間。返回重用營業時間計算功能的參數
問題是,我想爲每個消息創建不同的消息。
只要你知道,我的時間作爲一個對象從我的數據庫(例如進來:Object {Fri_c: "4:00 AM", Fri_o: "3:00 PM", Mon_c: "4:00 AM", Mon_o: "3:00 PM", Sat_c: "4:00 AM"…}
,這麼多我的功能是用來簡單地重新格式化信息,從而可以計算出
這裏的。到目前爲止我的代碼:
var bizHours = marker.businesshour,
hpyHours = marker.happyhour;
if (bizHours) {
var isOpen,
isClosed,
isClosedToday;
openClosed(bizHours, isOpen, isClosed, isClosedToday);
};
if (hpyHours) {
var isHappyHour,
notHappy,
noHappyHour;
openClosed(hpyHours, isHappyHour, notHappy, noHappyHour);
};
function openClosed(hours, opened, notopen, closedtoday) {
//CALCULATE SECONDS OPEN
var Open = hours[openKey],
splitOpenTime = Open.split(':'),
secondSplitOpenTime = splitOpenTime[1].split(" "),
secondsOpen;
if (secondSplitOpenTime[1] == 'PM' && splitOpenTime[0] != '12') {
secondsOpen = (+splitOpenTime[0] + 12) * 60 * 60 + (+secondSplitOpenTime[0]) * 60
} else {
secondsOpen = (+splitOpenTime[0]) * 60 * 60 + (+secondSplitOpenTime[0]) * 60
}
//CALCULATE SECONDS CLOSED
var CloseTime = hours[closeKey],
splitCloseTime = CloseTime.split(':'),
secondSplitCloseTime = splitCloseTime[1].split(" "),
secondsClose;
if (secondSplitCloseTime[1] == 'AM' && splitOpenTime[0] != '12') {
secondsClose = (+splitCloseTime[0]) * 60 * 60 + (+secondSplitCloseTime[0]) * 60
} else {
secondsClose = (+splitCloseTime[0] + 12) * 60 * 60 + (+secondSplitCloseTime[0]) * 60
}
//CALCULATE CURRENT-TIME IN SECONDS
var convertTime = time.split(':'),
secondsTime = (+convertTime[0]) * 60 * 60 + (+convertTime[1]) * 60;
//CALCULATE CLOSE AND CURRENT TIME IF DAY-CHANGED
if (secondsClose < secondsOpen) {
secondsClose = secondsClose + 86400
} else {
secondsClose = secondsClose
}
if (secondsTime < secondsClose && secondsClose < secondsOpen) {
secondsTime = secondsTime + 86400
} else {
secondsTime = secondsTime
}
// RETURN/HOISE CALCULATED RESULTS TO THE PARAMATERS
if (secondsOpen < secondsTime && secondsTime < secondsClose) {
return opened = 'Open until ' + closeTime;
} else if (Open == '') {
return notopen = weekday;
} else {
return closedtoday = 'closed at' + closeTime;
};
};
所以,在我最初的IF語句我設置我使用作爲該我希望能夠返回/葫蘆值我最初的變量函數的參數變量。