forked from lsvekis/JavaScript-Exercises-Book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple Task Timer
More file actions
47 lines (47 loc) · 1.24 KB
/
Simple Task Timer
File metadata and controls
47 lines (47 loc) · 1.24 KB
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
<!-- Objective: Build a simple task timer. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Task Timer</title>
</head>
<body>
<h2>Task Timer</h2>
<div id="timerDisplay">00:00:00</div>
<button id="startButton">Start</button>
<button id="pauseButton">Pause</button>
<button id="resetButton">Reset</button>
<script>
let timer = 0;
let interval;
const timerDisplay = document.getElementById('timerDisplay');
function updateDisplay() {
const hours = Math.floor(timer / 3600).toString().padStart(2, '0');
const minutes = Math.floor((timer % 3600) / 60).toString().padStart(2, '0');
const seconds = (timer % 60).toString().padStart(2, '0');
timerDisplay.textContent = `${hours}:${minutes}:${seconds}`;
}
document.getElementById('startButton').addEventListener('click', () => {
if (!interval) {
interval = setInterval(() => {
timer++;
updateDisplay();
}, 1000);
}
});
document.getElementById('pauseButton').addEventListener('click', () => {
if (interval) {
clearInterval(interval);
interval = null;
}
});
document.getElementById('resetButton').addEventListener('click', () => {
clearInterval(interval);
interval = null;
timer = 0;
updateDisplay();
});
updateDisplay(); // Initialize display
</script>
</body>
</html>