-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcount-down-timer.html
38 lines (35 loc) · 1.02 KB
/
count-down-timer.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Countdown Timer</title>
</head>
<body>
<!--
Create a countdown timer that starts from a specified number of seconds and updates every second.
Display an alert when the countdown reaches zero.
-->
<button onclick="startTimer()">Start Countdown</button>
<script>
function startTimer() {
let seconds = parseInt(
prompt("Enter the countdown duration in seconds:")
);
if (isNaN(seconds) || seconds <= 0) {
alert("Invalid input. Please enter a positive number.");
return;
}
const timer = setInterval(function () {
console.log(seconds);
if (seconds === 0) {
clearInterval(timer);
alert("Countdown reached zero!");
} else {
seconds--;
}
}, 1000);
}
</script>
</body>
</html>