commit 2437e14af87e64ce4fb832d5c75e23a44446bbf7
parent 4421a6d229ef441ce89fdd46f2792f0404bd6cd6
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Mon, 11 Mar 2024 18:11:06 -0700
Improve counter script
Pass arguments using setTimeout instead of using global variables
and log a warning if the #counter element was not found.
Diffstat:
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/scripts/counter.js b/scripts/counter.js
@@ -13,18 +13,20 @@ function sn(n) {
return String(n).padStart(2, "0")
}
-function updatetime() {
- let elapsed = gettime() - starttime
+function updatetime(element, start) {
+ let elapsed = gettime() - start
let mn = Math.trunc(elapsed / 60)
let sc = elapsed % 60
let hr = Math.trunc(mn / 60)
mn = mn % 60
- start.textContent = sn(hr) + ':' + sn(mn) + ':' + sn(sc)
+ element.textContent = sn(hr) + ':' + sn(mn) + ':' + sn(sc)
/* if (start.checkVisibility()) */
- setTimeout(updatetime, 1000)
+ setTimeout(updatetime, 1000, element, start)
}
let start = document.getElementById("counter")
-let starttime = gettime() - getduration(start.dateTime)
-
-updatetime()
+if (!start) {
+ console.warn("Expected #counter element")
+} else {
+ updatetime(start, gettime() - getduration(start.dateTime))
+}