Skip to content

Commit 8e17923

Browse files
committed
made countdown timer project & added readme
1 parent e9bbaf9 commit 8e17923

File tree

7 files changed

+96
-8
lines changed

7 files changed

+96
-8
lines changed

29-Countdown-Timer/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 29 - Countdown Timer
2+
3+
> Also added sound alert when the time get's up & made it responsive.
4+
5+
## Things I learned
6+
- `Date.now()` method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.**Useful for making countdown timer**
7+
- In `setInterval()` if we use it to countdown then sometimes it will stop on browser & iPhone for performance issues. So call it in a varibale instead of calling on self.
8+
- Make the structure & identify for which to make function.
9+
- `clearInterval()` to stop the interval.
10+
- Also learned about `new Date(timestamp)` that we can pass timestamp to Date object.
11+
- In this I learn how to deal with JavaScript parts with how to make functions & used them accordingly.
12+
13+
## Some important things
14+
- If remaining seconds or minutes are less than 10 then add 0 before it. `remainderSeconds < 10 ? '0' : ''`

29-Countdown-Timer/assets/alert.mp3

49.7 KB
Binary file not shown.
1.27 KB
Loading

29-Countdown-Timer/index.html

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<title>Countdown Timer</title>
99
<link rel="stylesheet" href="style.css">
10+
<link rel="shortcut icon" href="./assets/stopwatch.png" type="image/x-icon">
1011
<script src="main.js" defer></script>
1112
</head>
1213

1314
<body>
1415
<div class="timer">
1516
<div class="timer__controls">
16-
<button data-time="20" class="timer__button color">20 Secs</button>
17-
<button data-time="300" class="timer__button color">Work 5</button>
18-
<button data-time="900" class="timer__button color">Quick 15</button>
19-
<button data-time="1200" class="timer__button color">Snack 20</button>
20-
<button data-time="3600" class="timer__button color">Lunch Break</button>
17+
<button data-time="20" class="timer__button">20 Secs</button>
18+
<button data-time="300" class="timer__button">Work 5</button>
19+
<button data-time="900" class="timer__button">Quick 15</button>
20+
<button data-time="1200" class="timer__button">Snack 20</button>
21+
<button data-time="3600" class="timer__button">Lunch Break</button>
2122
<form name="customForm" id="custom">
2223
<input type="text" name="minutes" placeholder="Enter Minutes">
2324
</form>
2425
</div>
2526
<div class="display">
26-
<h1 class="display__time-left">Fuck You</h1>
27-
<p class="display__end-time">Some random text</p>
27+
<h1 class="display__time-left"></h1>
28+
<p class="display__end-time"></p>
2829
</div>
2930
</div>
31+
<audio class="alert" src="./assets/alert.mp3" hidden></audio>
3032
</body>
3133

3234
</html>

29-Countdown-Timer/main.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
let countDown;
2+
const displayTime = document.querySelector('.display__time-left');
3+
const endTime = document.querySelector('.display__end-time');
4+
const buttons = document.querySelectorAll('[data-time]');
5+
const alert = document.querySelector('.alert');
6+
7+
function timer(seconds) {
8+
clearInterval(countDown);
9+
10+
const now = Date.now();
11+
const then = now + (seconds*1000);
12+
displayTimeLeft(seconds);
13+
displayEndTime(then);
14+
15+
countDown = setInterval(() => {
16+
const secondsLeft = Math.round((then - Date.now())/1000);
17+
if(secondsLeft < 0) {
18+
alert.currentTime = 0;
19+
alert.play();
20+
clearInterval(countDown);
21+
return;
22+
}
23+
displayTimeLeft(secondsLeft);
24+
}, 1000);
25+
}
26+
27+
function displayTimeLeft(seconds) {
28+
const minutes = Math.floor(seconds/60);
29+
const remainderSeconds = seconds%60;
30+
// if remaining seconds are less than 10 then add 0 before it.
31+
const display = `${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`;
32+
document.title = `Time Left: ${display}`;
33+
displayTime.textContent = display;
34+
}
35+
36+
function displayEndTime(timestamp) {
37+
const end = new Date(timestamp);
38+
const hours = end.getHours();
39+
const minutes = end.getMinutes();
40+
const adjustHours = hours > 12 ? hours - 12 : hours;
41+
endTime.textContent = `Be back at ${adjustHours}:${minutes < 10 ? '0':''}${minutes}`;
42+
}
43+
44+
function startTimer() {
45+
const seconds = parseInt(this.dataset.time);
46+
timer(seconds);
47+
}
48+
49+
buttons.forEach(button => button.addEventListener('click', startTimer));
50+
51+
document.customForm.addEventListener('submit', function(e) {
52+
e.preventDefault();
53+
const minutes = parseInt(this.minutes.value);
54+
timer(minutes*60);
55+
this.reset();
56+
})

29-Countdown-Timer/style.css

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ body {
5555
padding: 2rem;
5656
}
5757

58+
input {
59+
font-size: 1.5rem;
60+
}
61+
5862
.timer__button {
5963
background: none;
6064
border: 0;
@@ -81,4 +85,14 @@ body {
8185
align-items: center;
8286
flex-direction: column;
8387
flex: 1;
88+
}
89+
90+
/* added for making it responsive */
91+
@media (max-width: 50rem) {
92+
.timer__controls {
93+
flex-direction: column;
94+
}
95+
.display h1{
96+
font-size: 15rem;
97+
}
8498
}

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| 20 | 🗣 Speech Detection | [GitHub][20] | [Link](https://sagarmittal1.github.io/JavaScript30/20-Speech-Detection/)|
2424
| 21 | 📌 Geolocation | [GitHub][21] | [Link](https://sagarmittal1.github.io/JavaScript30/21-Geolocation/)|
2525
| 23 | 💬 Speech Synthesis | [GitHub][23] | [Link](https://sagarmittal1.github.io/JavaScript30/23-Speech-Synthesis/)|
26+
| 29 | ⏱ Countdown Timer | [GitHub][29] | [Link](https://sagarmittal1.github.io/JavaScript30/29-Countdown-Timer/)|
2627

2728

2829
[1]: /01-JavaScript-Drum-Kit/
@@ -40,4 +41,5 @@
4041
[19]: /19-Webcam-Fun/
4142
[20]: /20-Speech-Detection/
4243
[21]: /21-Geolocation/
43-
[23]: /23-Speech-Synthesis/
44+
[23]: /23-Speech-Synthesis/
45+
[29]: /29-Countdown-Timer/

0 commit comments

Comments
 (0)