营业时间状态

时间:2013-10-18 16:17:08

标签: javascript jquery

我创建了这个小对象,让你知道一个地方是否基于两个变量打开。但是我想让这更好,因为我不是很擅长javascript我想知道什么是最好的做法。如何在关闭或打开前一分钟到达那里的用户实时更新打开/关闭状态。

CodePen Source

HTML

<h1 class="status"></h1>
<h2 class="hours"></h2>

CSS

body {
  color: white; 
  text-align: center;
  font-size: 2em;
}

.open   { background-color: #00a638; }
.closed { background-color: black; }

JS

// variables
var open = "7:00";
var close = "5:00";

// setup miliary time
var mClose = close.replace(/:/g,'') + 1200;
var mOpen = open.replace(/:/g,'');

if (mOpen < 1000) {
  mOpen = "0" + mOpen;
}

// setup hours
var now = new Date();
var hour = now.getHours();
if (hour >= 10) { mHour = hour; 
  } else { mHour = "0" + hour; }

// setup minutes
var minute = now.getMinutes();

if (minute < 10) {
  minute = "0" + minute;
}

var time = "" + mHour + minute;
console.log("Time is " + time);

// open/close status
if ((time >= mOpen) && (time < mClose)) {
 $("body").addClass("open");
 $(".status").html("Yes, We're Open");
} else {
 $("body").addClass("closed");   
 $(".status").html("Sorry We're Closed");
}

// display hours
$(".hours").html(open + " to " + close);

0 个答案:

没有答案