summaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-06-07 21:00:17 -0400
committerDavid Gay <david@davidgay.org>2021-06-07 21:00:17 -0400
commitb6955ee9aa0058f64f6e3b6314d2b7358939bc2e (patch)
tree4b088bea5f6886650fdc4ea66e873ef2e98026af /public
parentbafe2ca25dcb347bc00cbae5cb82d5658a92ec4f (diff)
Implement web worker to keep timer running even when page is inactive
Diffstat (limited to 'public')
-rw-r--r--public/timer_worker.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/public/timer_worker.js b/public/timer_worker.js
new file mode 100644
index 0000000..3d3e79a
--- /dev/null
+++ b/public/timer_worker.js
@@ -0,0 +1,24 @@
+let timerInterval = null;
+
+function setTimerInterval() {
+ if (!timerInterval) {
+ timerInterval = setInterval(function() {
+ postMessage("timerTick");
+ }, 10);
+ }
+}
+
+function clearTimerInterval() {
+ clearInterval(timerInterval);
+ timerInterval = null;
+}
+
+onmessage = function(event) {
+ if ("run_flag" in event.data) {
+ if (event.data["run_flag"]) {
+ setTimerInterval();
+ } else {
+ clearTimerInterval();
+ }
+ }
+}