Managing school bell schedules can be challenging, especially when there are different schedules for different days. In this case, we have two arrays containing sets of dates and times for a school bell schedule. The challenge is to pull the array for Wednesdays only on Wednesdays, while allowing the other array to work on all other days besides Wednesday. The purpose of this code is to display a countdown timer if the current time falls between the start and end times specified in the array.
//start of code
var d = new Date(2019);
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
return {
'total': t,
'minutes': minutes,
'seconds': seconds
};
}
var myVar;
myVar = setInterval(function(){window.location.reload(1);}, 1000);
function myStopFunction() {
clearTimeout(myVar);
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
clock.style.display = 'block';
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var windowObjectReference;
function openRequestedPopup() {
windowObjectReference = window.open(
"file:///C:/Users/Darren%20Blount/OneDrive/COUNTDOWN/5_timer.html",
"DescriptiveWindowName", 'height=30000,width=40000, left=40000, resizable=yes,scrollbars=yes,toolbar=yes, status=yes');
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"];
var d = new Date();
var currentMonth = monthNames[d.getMonth()];
var DayNames = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31"];
var g = new Date();
var currentDay = DayNames[g.getDay()];
var wednesday /* Havenot worked into code*/= [
// Daily Bell Schedule
['Oct 30, 2018 7:50:49', 'Oct 30, 2018 8:00:49'],
//AAP
['Oct 30, 2018 9:29:49', 'Oct 30, 2018 9:34:49'],
//1st Block - 2nd Block
['Oct 30, 2018 9:58:49', 'Oct 30, 2018 10:03:49'],
//2nd Block - 1st Half 3rd Block
['Oct 30, 2018 11:49:49', 'Oct 30, 2018 11:37:49'],
//1st Lunch - 3rd Block
['Oct 30, 2018 12:02:49', 'Oct 30, 2018 12:07:49'],
//2nd Lunch - 2nd Half of 3rd Block
['Oct 30, 2018 12:42:49', 'Oct 30, 2018 12:47:49'],
//3rd Block - 4th Block
['Oct 30, 2018 13:36:49', 'Oct 30, 2018 13:41:49']
];
const schedule /*Everyday*/= [
['Sep 11, 2018 7:50:49', 'Sep 11, 2018 8:00:49'],
//AAP
['Sep 11, 2018 9:29:49', 'Sep 11, 2018 9:34:49'],
//1st Block - 2nd Block
['Sep 11, 2018 9:58:49', 'Sep 11, 2018 10:03:49'],
//2nd Block - 1st Half 3rd Block
['Sep 11, 2018 11:49:49', 'Sep 11, 2018 11:37:49'],
//1st Lunch - 3rd Block
['Sep 11, 2018 12:02:49', 'Sep 11, 2018 12:07:49'],
//2nd Lunch - 2nd Half of 3rd Block
['Sep 11, 2018 12:42:49', 'Sep 11, 2018 12:47:49'],
//3rd Block - 4th Block
['Sep 11, 2018 1:36:49', 'Sep 11, 2018 1:41:49']
];
const schedule = [
[currentMonth + currentDay + '2019 13:40:00', currentMonth + currentDay
+ '2019 14:30:00']
]
for(let i=0; i<schedule.length; i++){
// pull them straight into Date objects
const startDate = new Date(schedule[i][0]);
const endDate = new Date(schedule[i][1]);
// Make a new Date for setting it for today, then set the hours based off
the schedule
let startTime = new Date();
startTime.setHours(startDate.getHours(), startDate.getMinutes(),
startDate.getSeconds());
let endTime = new Date();
endTime.setHours(endDate.getHours(), endDate.getMinutes(),
endDate.getSeconds());
// Easier way to just get the ms and then the same check
const currentMs = Date.now();
if(endTime.valueOf() > currentMs && currentMs >= startTime.valueOf() ){
initializeClock('clockdiv', endDate);
openRequestedPopup();
myStopFunction();
setInterval(function(){window.location.reload(5);}, 306000);
setTimeout(function () { windowObjectReference.close();}, 305000);
}
}