Simplified clock.

This commit is contained in:
Marat Fayzullin 2023-06-30 11:25:59 -04:00
parent 8fe33a3b81
commit eee5b68d6b
1 changed files with 8 additions and 21 deletions

View File

@ -1,32 +1,19 @@
function Clock(el) {
const now = new Date();
const me = this;
// Run timer for the first time on the next minute change
setTimeout(function() { me.update(); }, 1000 * (60 - now.getUTCSeconds()));
// Save HTML element to update
this.el = el;
this.timer = null;
// Draw for the first time
this.draw();
// Update for the first time
this.update();
}
Clock.prototype.update = function() {
// Schedule timer in one minute intervals
if (!this.timer) {
const now = new Date();
const me = this;
this.timer = setInterval(function() { me.update(); }, 60000);
}
// Display UTC clock
this.draw();
}
// Next update at the next minute change
setTimeout(function() { me.update(); }, 1000 * (60 - now.getUTCSeconds()));
Clock.prototype.draw = function() {
// Display UTC clock
if (this.el) {
const now = new Date();
const hours = ("00" + now.getUTCHours()).slice(-2);
const minutes = ("00" + now.getUTCMinutes()).slice(-2);
this.el.html(`${hours}:${minutes} UTC`);