-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.html
More file actions
66 lines (61 loc) · 1.94 KB
/
timer.html
File metadata and controls
66 lines (61 loc) · 1.94 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
#timerDisplay {
font-size: 2em;
margin-bottom: 10px;
}
input, button {
padding: 10px;
margin: 5px;
font-size: 1em;
}
</style>
</head>
<body>
<div>
<div id="timerDisplay">00:00</div>
<input type="number" id="secondsInput" placeholder="Enter seconds">
<button onclick="startTimer()">Start Timer</button>
</div>
<script>
let countdown;
function startTimer() {
let seconds = parseInt(document.getElementById("secondsInput").value);
if (isNaN(seconds) || seconds <= 0) {
alert("Please enter a valid number of seconds.");
return;
}
clearInterval(countdown);
const timerDisplay = document.getElementById("timerDisplay");
timerDisplay.textContent = formatTime(seconds);
countdown = setInterval(() => {
seconds--;
timerDisplay.textContent = formatTime(seconds);
if (seconds <= 0) {
clearInterval(countdown);
alert("Time's up!");
}
}, 1000);
}
function formatTime(seconds) {
let minutes = Math.floor(seconds / 60);
let remainingSeconds = seconds % 60;
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
</script>
</body>
</html>