From b6955ee9aa0058f64f6e3b6314d2b7358939bc2e Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 7 Jun 2021 21:00:17 -0400 Subject: Implement web worker to keep timer running even when page is inactive --- public/timer_worker.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 public/timer_worker.js (limited to 'public') 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(); + } + } +} -- cgit v1.2.3