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