返回参数以重用营业时间计算功能

时间:2016-01-08 16:31:53

标签: javascript function-parameter

我正在开发一个提供当地餐馆信息的应用程序。所有餐厅都有营业时间。只有部分餐厅有欢乐时光。我希望能够重新使用我今天确定开放,当前关闭或关闭的功能,以确定欢乐时光时间和营业时间时间,而不是两次使用相同的代码块。

问题是,我想为每个人创建不同的消息。

您知道,我的工作时间是从我的数据库中作为对象进入的(例如: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 个答案:

答案 0 :(得分:1)

if(bizHours) / if(hpyHours)块中,您使用不同的参数调用openClosed()函数,此时其中3个为空。

如果您已将对象定义为“标记”,则无需执行此操作:

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

而是检查if语句中每个参数的状态:

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

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

现在你必须考虑你想要允许的状态。或者/或者可能是真的,你可能不希望这取决于你想要做什么。

截至不同的消息并重用相同的代码,我认为这样可行:

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();就行了。