2016-01-08 51 views
1

我正在開發一個應用程序,提供有關當地餐館的信息。所有的餐廳都有營業時間。只有一些餐館有歡樂時光。我不希望兩次使用同一代碼塊,而是希望能夠重用我的功能,以確定今天的開放時間,當前關閉時間或關閉時間,以確定歡樂時間和營業時間。返回重用營業時間計算功能的參數

問題是,我想爲每個消息創建不同的消息。

只要你知道,我的時間作爲一個對象從我的數據庫(例如進來: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語句我設置我使用作爲該我希望能夠返回/葫蘆值我最初的變量函數的參數變量。

回答

1

在你的if(bizHours)/if(hpyHours)大塊您可以使用不同的參數調用您的openClosed()函數,其中3個在此處爲空。

如果您已經定義爲「標記」的對象,你並不需要這樣做:

if(marker.businesshour){ 
    ... 
}; 

if(marker.happyhour){ 
    ... 
}; 

var bizHours = marker.businesshour, 
hpyHours = marker.happyhour; 

而不是你的if語句檢查每個參數的狀態

現在你必須考慮你想允許的狀態。要麼/或可能是真實的,你可能不希望這取決於你想要做什麼。

由於不同的消息,並且重複使用相同的代碼,我認爲這會工作:

var marker = {businesshour:null, happyhour:null}; 

function go() 
{ 
    if (marker.businesshour) { 
     alert("call your time function and pass marker.businesshour"); 
    }; 

    if (marker.happyhour) { 
     alert("call your time function and pass marker.happyhour"); 
    }; 

    return marker; 
} 

如果你在Firefox的控制檯加載此,並設置marker.businesshour爲1,並調用go();它的工作原理。