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) {
var isClosed = true;
for (var i = 0; i < data.location.monday.length || isClosed; i++) {
console.log("sucess1");
xr_mon= data.location.monday[i];
console.log("sucess2");
if (locTime >= xr_mon.kai && locTime < xr_mon.guan ){
console.log("location is open!");
isClosed = false;
$('.xr').show("drop", { direction: "up" }, 400);
}
else {
console.log("location is closed");
$('.xr').hide("drop", {
direction: "down"
}, 800);
}
}
});
}
);
});
</script>
JSON 代码
{ "location":
{
"monday": [
{"kai": 700, "guan": 1400},
{"kai": 1700, "guan": 2100}
]
}
}
经过几次尝试这个循环序列。
即使 isClosed 满足条件,我仍然无法弄清楚为什么它仍在循环。我错过了我的代码中的任何内容吗???
找到了我的答案。
<?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) {
for (var i = 0; i < data.location.monday.length; i++) {
console.log("sucess1");
xr_mon= data.location.monday[i];
console.log("sucess2");
if (locTime >= xr_mon.kai && locTime < xr_mon.guan ){
console.log("location is open!");
$('.xr').show("drop", { direction: "up" }, 400);
Break;
}
else {
console.log("location is closed");
$('.xr').hide("drop", {
direction: "down"
}, 800);
}
}
});
}
);
});
</script>