-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer-worker.js
More file actions
48 lines (40 loc) · 1022 Bytes
/
timer-worker.js
File metadata and controls
48 lines (40 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Web Worker for accurate background timer
let timerInterval;
let remainingSeconds;
self.onmessage = function(e) {
const { action, seconds } = e.data;
if (action === 'start') {
remainingSeconds = seconds;
// Clear any existing interval
if (timerInterval) {
clearInterval(timerInterval);
}
// High-precision timer that works in background
timerInterval = setInterval(() => {
remainingSeconds--;
// Send update back to main thread
self.postMessage({
type: 'tick',
seconds: remainingSeconds
});
if (remainingSeconds <= 0) {
clearInterval(timerInterval);
self.postMessage({
type: 'complete'
});
}
}, 1000);
}
if (action === 'stop') {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
}
if (action === 'getRemaining') {
self.postMessage({
type: 'remaining',
seconds: remainingSeconds
});
}
};