-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (78 loc) · 3.13 KB
/
script.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let isPaused = false;
let lifePoints, timer;
let intervalId = null;
document.getElementById('startButton').addEventListener('click', function() {
let inputVal = document.getElementById('lifePointsInput').value;
if (inputVal) {
lifePoints = parseInt(inputVal);
timer = 10 * 60; // Reset timer to 10 minutes
document.getElementById('lifePointsDisplay').textContent = lifePoints;
isPaused = false;
}
startTimer();
document.getElementById('pauseResumeButton').textContent = 'Pause';
document.getElementById('pauseResumeButton').style.display = 'inline';
});
document.getElementById('pauseResumeButton').addEventListener('click', function() {
if (isPaused) {
startTimer();
this.textContent = 'Pause';
} else {
clearInterval(intervalId);
this.textContent = 'Resume';
}
isPaused = !isPaused;
});
function startTimer() {
if (intervalId) clearInterval(intervalId);
intervalId = setInterval(function() {
if (!isPaused && lifePoints > 0 && timer > 0) {
timer--;
updateTimerDisplay(timer);
if (timer === 0) {
lifePoints--;
document.getElementById('lifePointsDisplay').textContent = lifePoints;
timer = 10 * 60; // Reset timer
}
} else if (lifePoints <= 0) {
clearInterval(intervalId);
document.getElementById('pauseResumeButton').style.display = 'none';
}
localStorage.setItem('lifePointsAppState', JSON.stringify({ lifePoints, timer, lastUpdateTime: new Date(), isPaused }));
}, 1000);
}
function updateTimerDisplay(timer) {
let minutes = Math.floor(timer / 60);
let seconds = timer % 60;
document.getElementById('timerDisplay').textContent = minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
}
document.addEventListener('DOMContentLoaded', function() {
let savedState = localStorage.getItem('lifePointsAppState');
if (savedState) {
savedState = JSON.parse(savedState);
lifePoints = savedState.lifePoints;
isPaused = savedState.isPaused;
const lastUpdateTime = new Date(savedState.lastUpdateTime);
const currentTime = new Date();
const elapsedSeconds = Math.floor((currentTime - lastUpdateTime) / 1000);
timer = Math.max(savedState.timer - elapsedSeconds, 0);
while (timer <= 0 && lifePoints > 0) {
lifePoints--;
timer += 10 * 60; // Add 10 minutes in seconds
}
} else {
lifePoints = 96; // Default Life Points
timer = 10 * 60; // 10 minutes in seconds
isPaused = false;
}
document.getElementById('lifePointsDisplay').textContent = lifePoints;
updateTimerDisplay(timer);
if (!isPaused) {
startTimer();
document.getElementById('pauseResumeButton').style.display = 'inline';
document.getElementById('pauseResumeButton').textContent = 'Pause';
} else {
document.getElementById('pauseResumeButton').style.display = 'inline';
document.getElementById('pauseResumeButton').textContent = 'Resume';
}
});