Вот простое решение, которое может помочь вам указать правильное направление.
Я думаю, что одна из проблем с вашим кодом состоит в том, что он захватывает только час дня, а не день недели.
Ниже вы можете установить свои дни и часы работы в объекте open
, но если у вас будет другое время открытия в разные дни в будущем, вам нужно будет определить объект open
по-разному и будет иметьизменить как работает функция getStatus
// set up the interval so that the time can be started and stopped as needed
var interval;
// set the days and times when open (this could be set up differently, for example it could be a range instead)
var open = {
days: [1, 2, 3, 4, 5],
hours: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
}
// given a date, return a message determining if open
var getStatus = function(currentDate){
var hour = currentDate.getHours();
var day = currentDate.getDay();
var nowIsOpenDay = open.days.indexOf(day) > -1;
var nowIsOpenHour = open.hours.indexOf(hour) > -1;
var message = (nowIsOpenDay && nowIsOpenHour) ? 'Currently opened' : 'Currently closed';
return {
'message': message,
'dateInfo': {
'hour': hour,
'day': day,
'nowIsOpenDay': nowIsOpenDay,
'nowIsOpenHour': nowIsOpenHour
}
}
}
// run the timer and get the current status
var startInterval = function(updateInterval){
updateInterval = (typeof updateInterval === 'undefined') ? 1000 : updateInterval;
interval = setInterval(function(){
var currentStatus = getStatus(new Date());
console.log(currentStatus.message)
console.log(currentStatus.dateInfo.hour, currentStatus.dateInfo.day)
}, updateInterval);
}
// optionall stop the interval
var stopInterval = function(){
clearInterval(interval);
}
// start
startInterval(2000);