General points.
- Avoid redundant comments.
- Never add redundant code. The last line of the interval callback does nothing. Why is it there???
- Don't add text to elements via
innerHTML
use textContent
.
- Reserve the names
element
, elements
for HTMLElements
don't use them for string, numbers, arrays, etc...
- Use 'const' for all variables that do not change.
- Avoid
setInterval
as it is dangerous power hungry memory leak waiting to happen.
Don't abuse the client.
When you have code running on a client you are using their resources which they pay for.
Always write code to be as performant and efficient as possible.
Use memory to store repeated operations that produce the same result. Eg query document once not each time the same element is needed.
Never run code if there is no need.
You show seconds yet you update 200ms, 5 times above the required rate.
Set Interval is automatically throttled when focus is lost (Because of its wide spread abuse) however what rate it runs at is determined by the browser. Best to actively control the rate of updates depending on focus/visibility.
Rewrites
First example uses focus and blur event to set update rates.
Second example use visibilitychange
change events to turn on and off updates.
The ideal is to use both focus
/blur
and visibilitychange
to speed up, slowdown and stop updates.
Example 1
Slows update rate when focus is lost (note that lost focus does not mean that the page is not visible).
;(()=>{
"use strict";
const LOCALE_NUM = ['en-EN', {minimumIntegerDigits: 2, useGrouping: false}];
const MIN_RATE = 5000;
const MAX_RATE = 1000;
const timeEl = document.querySelector("#clockEl");
const dateEl = document.querySelector("#dateEl");
var currentRate = MAX_RATE, updateHdl;
addEventListener("focus", () => { currentRate = MAX_RATE; updateTime();} );
addEventListener("blur", () => currentRate = MIN_RATE);
const doubleDigits = num => num.toLocaleString(...LOCALE_NUM);
const showArrVals = (el, vals, sep) => { el.textContent = vals.map(n => doubleDigits(n)).join(sep); }
updateTime();
function updateTime() {
clearTimeout(updateHdl);
const date = new Date();
showArrVals(timeEl, [date.getHours(), date.getMinutes(), date.getSeconds()], ':');
showArrVals(dateEl, [date.getUTCFullYear(), date.getMonth() + 1, date.getDate()], '-');
updateHdl = setTimeout(updateTime, currentRate);
}
})();
Example 2
Constant rate time display will stop when page is hidden.
;(()=>{
"use strict";
const LOCALE_NUM = ['en-EN', {minimumIntegerDigits: 2, useGrouping: false}];
const RATE = 1000;
const timeEl = document.querySelector("#my_clock");
const dateEl = document.querySelector("#my_date");
var updateHdl;
document.addEventListener("visibilitychange", () => {
document.visibilityState === "hidden" ? clearTimeout(updateHdl) : updateTime();
});
const doubleDigits = num => num.toLocaleString(...LOCALE_NUM);
const showArrVals = (el, vals, sep) => { el.textContent = vals.map(n => doubleDigits(n)).join(sep); }
updateTime();
function updateTime() {
const date = new Date();
showArrVals(timeEl, [date.getHours(), date.getMinutes(), date.getSeconds()], ':');
showArrVals(dateEl, [date.getUTCFullYear(), date.getMonth() + 1, date.getDate()], '-');
updateHdl = setTimeout(updateTime, RATE);
}
})();