JSFiddle - React, Tailwind, and code Playground
by Olayinka Otuniyi
JavaScript
function DeltaTimer(render, interval) {
var timeout;
var lastTime;
this.start = start;
this.restart = restart;
this.stop = stop;
this.loop = loop;
function start() {
timeout = setTimeout(loop, 0);
lastTime = Date.now();
return lastTime;
}
function restart() {
timeout = setTimeout(loop, 1.8e+6);
lastTime = Date.now();
return lastTime;
}
function stop() {
clearTimeout(timeout);
return lastTime;
}
function loop() {
var thisTime = Date.now();
var deltaTime = thisTime - lastTime;
var delay = Math.max(interval - deltaTime, 0);
timeout = setTimeout(loop, delay);
lastTime = thisTime + delay;
render(thisTime);
}
}
var times = {
timing: function() {
var thisTime = (new Date).toLocaleTimeString('en-GB');
return thisTime;
},
timeSQLCalendarChange: function() {
var endTime = "16:30:00";
return endTime;
},
today: function() {
var thisDay = (new Date).getDay();
return thisDay;
},
dateSQL: function() {
var today = new Date();
var date = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
return date;
},
paramClosingTime: function() {
var closingTime = "16:15:00";
if (times.today() === 5) {
var closingTime = "12:30:00";
}
return closingTime;
},
calendarDate: function() {
var now = (new Date).toDateString('en-GB');
return now;
}
}
//Updates
var updateTimes = new DeltaTimer(
function(time) {
times.timing();
times.dateSQL();
times.today();
times.paramClosingTime();
}, 1000);
updateTimes.start();