0

JS

<?php
    date_default_timezone_set('America/Los_Angeles');
    $date = date('Gi', time());
?>

<script type="text/javascript">
$(function() {
  $(".show_hide").click( function()
       {
        var locTime = <?php echo json_encode($date) ?>;

$.getJSON( "url_to_json", function(data) {

    xr_mon_b_o = data.location.monday[0].b_o;
    xr_mon_l_c = data.location.monday[0].l_c;
    xr_mon_d_o = data.location.monday[0].d_o;
    xr_mon_d_c = data.location.monday[0].d_c;
    console.log("sucess1");
    if (locTime < xr_mon_b_o){
        console.log("sucess2");
        $('.xr').hide("drop", { direction: "down" }, 400); 
        }

    else if ( xr_mon_d_o < locTime > xr_mon_l_c){
        console.log("sucess4");
        $('.xr').hide("drop", { direction: "down" }, 400);
        }

    else if ( locTime > xr_mon_d_c){
        console.log("sucess4");
        $('.xr').hide("drop", { direction: "down" }, 400);
        }

        else {
        $('.s_lo').show("drop", {
              direction: "up"
          }, 800);
        }

});
       }
  );
});

json

{ "location": 
{
    "monday": [
    {"b_o": 700},
    {"l_c": 1400},
    {"d_o": 1700},
    {"d_c": 2100}

    ]
}
}

b = 早餐

l = 午餐

d = 晚餐

o = 打开

c = 关闭

我正在尝试使此脚本在时间落入 else if 参数的情况下工作,并通过 jquery 操作触发关闭的位置 div 隐藏。但不知何故,这个脚本中的逻辑似乎不能很好地工作。它适用于 1 if,但是当我添加更多“else if”时,脚本会崩溃。

4

1 回答 1

0

你的 JSON 有点复杂。这是我要遵循的策略:

<script type="text/javascript">
$(function(){
    $(".show_hide").click(function(){
        var current_time = <?php echo intval($date) ?>;

        $.getJSON("url_to_json",function(data){
            /**
             * Assume the array is always ordered
             */
            var day = data.location.monday[0],
            time_diff = 2400, // Time difference between a shift, and now
            current_diff, // Auxiliar variable
            current_shift; // The current shift

            /**
             * Compare all shifts
             */
            for (shift in day) {
                /**
                 * If current time is exactly the same
                 * as the shift, we found the shift
                 */
                if (current_time == day[shift]) {
                    time_diff = current_diff;
                    current_shift = shift;
                    break;
                }
                else if (current_time > day[shift]) {
                    /**
                     * Calculate the time difference
                     */
                    current_diff = current_time - day[shift];

                    /**
                     * The shift with the minor time difference
                     * is the active shift
                     */
                    if (current_diff < time_diff) {
                        time_diff = current_diff;
                        current_shift = shift;
                    }
                }
            }

            /**
             * shift is now determined, think what to do
             * based on shift letters:
             *
             * (b|l|d)_(o|c)
             */
            if (shift.match(/b_/)) {
                // Breakfast
                if (shift.match(/_o/)) {
                    // Open
                }
                else {
                    // Closed
                }
            }
            else if (shift.match(/l_/)) {
                // Lunch
            }
            else {
                // Dinner
            }
        });
    });
});
于 2013-06-26T00:35:54.513 回答