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 =关闭
我试图让这个脚本工作,如果时间落入其他地方,如果是arugement并通过jquery动作触发关闭的位置div隐藏。但不知何故,这个剧本中的逻辑似乎并没有很好地运作。它可以正常使用1,如果,但随着我添加更多&#39;否则如果&#39;剧本分崩离析。
答案 0 :(得分: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
}
});
});
});