在特定时间或某天显示某些信息。
我对jquery很新,并且我的代码目前正在工作,因此假设商店每周的每一天都是从晚上9点到晚上8点开放。感谢任何帮助,谢谢
但是,如何调整我的代码,以便开放时间为:
SCRIPT:
var data = [
[0, 9, "Sorry we are closed."],
[10, 20, "We are open."],
[21, 24, "Sorry, we are closed."]
],
hr = new Date().getHours();
for(var i=0; i<data.length;i++){
if(hr >= data[i][0] && hr <= data[i][1]){
$("#open_close").html(data[i][2]);
break;
}
};
HTML:
<div class="time"><a href="#contact">
<img id="clock" src="assets/clock.png">
<div id="open_close"></div></a>
</div>
答案 0 :(得分:2)
我很想以不同的方式构建您的数据,以简化您使用的逻辑......
var data = [
{}, //Sunday - closed
{ open: 18, close: 22 }, //Monday
{ open: 18, close: 22 }, //Tuesday
{ open: 18, close: 22 }, //Wednesday
{ open: 11, close: 22 }, //Thursday
{ open: 11, close: 22 }, //Friday
{} //Saturday - closed
];
然后你可以让你的逻辑更简单......
var date = new Date();
var dayOfWeek = date.getDay(); // 0 is Sunday, 1 is Monday, etc...
var openingTimes = data[dayOfWeek];
var openClosed = false; // closed by default
// check that there are opening times for today
if (openingTimes.hasOwnProperty('open') && openingTimes.hasOwnProperty('close')){
var hour = date.getHours()
openClosed = openingTimes.open <= hour && hour < openingTimes.close;
}
$("#open_close").html(openClosed ? 'We are open': 'Sorry, we are closed.');