From 620c850966718ee6d8357a08068d62c4813bffe8 Mon Sep 17 00:00:00 2001 From: Eric Fredricksen Date: Thu, 9 Dec 2021 13:00:32 -0800 Subject: [PATCH] Use web worker for timer so progress can take place in the background --- main.html | 2 -- main.js | 27 ++++++++++++++++++--------- progros.css | 1 + 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/main.html b/main.html index 772b17c..23d492c 100644 --- a/main.html +++ b/main.html @@ -186,8 +186,6 @@ -
-

PAUSED
diff --git a/main.js b/main.js index 7843085..e38c9e4 100644 --- a/main.js +++ b/main.js @@ -1,22 +1,32 @@ // Copyright (c)2002-2010 Eric Fredricksen all rights reserved var game = {}; -var lasttick, timerid; +var clock; function timeGetTime() { return new Date().getTime(); } function StartTimer() { - if (!timerid) { - lasttick = timeGetTime(); - timerid = setTimeout(Timer1Timer, 100); + if (!clock) { + clock = new Worker('clock.js'); + clock.addEventListener('message', e => { + Timer1Timer(); + clock.lasttick = timeGetTime(); + }); + } + if (!clock.running) { + clock.lasttick = timeGetTime(); + clock.running = true; + clock.postMessage('start'); } } function StopTimer() { - clearTimeout(timerid); - timerid = null; + if (clock) { + clock.postMessage('stop'); + clock.running = false; + } } function Q(s) { @@ -878,7 +888,6 @@ function Pos(needle, haystack) { var dealing = false; function Timer1Timer() { - timerid = null; // Event has fired if (TaskBar.done()) { game.tasks += 1; game.elapsed += TaskBar.Max().div(1000); @@ -916,7 +925,7 @@ function Timer1Timer() { Dequeue(); } else { - var elapsed = timeGetTime() - lasttick; + var elapsed = timeGetTime() - clock.lasttick; if (elapsed > 100) elapsed = 100; if (elapsed < 0) elapsed = 0; TaskBar.increment(elapsed); @@ -1136,7 +1145,7 @@ function FormKeyDown(e) { } if (key === 'p') { - if (timerid) { + if (clock && clock.running) { $('#paused').css('display', 'block'); StopTimer(); } else { diff --git a/progros.css b/progros.css index e2f5b37..2dd44fe 100644 --- a/progros.css +++ b/progros.css @@ -109,6 +109,7 @@ body { -moz-background-size: 100% 100%; background-attachment: fixed; text-align: center; + margin-top: 1em; } div.scroll {