Skip to content

Commit 7283d60

Browse files
committed
Set Timer Dynamically & SetInterval Update
1 parent 239d1d9 commit 7283d60

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

countdown-timer/app.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,64 @@ const deadline = document.querySelector(".deadline");
2626
const presentation = document.querySelector(".presentation");
2727
const deadlineFormats = document.querySelectorAll(".deadline-format h4");
2828

29-
let pptDate = new Date(2020, 5, 26, 5, 30);
29+
// taking dates dynamically: whenever the application is opened, it sets timer to 10 days after the current date.
30+
let tempDate = new Date();
31+
let tempYear = tempDate.getFullYear();
32+
let tempMonth = tempDate.getMonth();
33+
let tempDay = tempDate.getDate();
34+
const pptDate = new Date(tempYear, tempMonth, tempDay + 10, 15, 30, 0);
35+
36+
// let pptDate = new Date(2020, 5, 27, 15, 30, 0); // static date values
37+
3038
const year = pptDate.getFullYear();
3139
const day = pptDate.getDate();
3240
const month = months[pptDate.getMonth()];
3341
const weekday = weekdays[pptDate.getDay()];
3442
let hours = pptDate.getHours();
35-
let text = "am";
36-
if (hours > 12) {
43+
if (hours >= 12) {
3744
hours -= 12;
38-
text = "pm";
3945
}
40-
const minutes = pptDate.getMinutes();
41-
presentation.textContent = `PPT begins on ${weekday} ${day} ${month} ${year}, ${hours}:${minutes}${text}`;
46+
let minutes = pptDate.getMinutes();
47+
if (hours < 10) {
48+
hours = `0${hours}`;
49+
}
50+
if (minutes < 10) {
51+
minutes = `0${minutes}`;
52+
}
53+
presentation.textContent = `PPT begins on ${weekday} ${day} ${month} ${year}, ${hours}:${minutes} hrs.`;
4254

43-
const pptTime = pptDate.getTime();
44-
getRemaindingTime = () => {
45-
const currentTime = new Date().getTime();
55+
const pptTime = pptDate.getTime(); // returns miliseconds
4656

47-
const time = pptTime - currentTime; // returns miliseconds
57+
let countdown = setInterval(getRemainingTime = () => {
58+
const currentTime = new Date().getTime();
59+
const time = pptTime - currentTime;
4860

61+
// convert miliseconds to required values
4962
const oneDay = 24 * 60 * 60 * 1000;
5063
const oneHour = 60 * 60 * 1000;
5164
const oneMinute = 60 * 1000;
52-
5365
const daysLeft = Math.floor(time / oneDay);
5466
const hoursLeft = Math.floor((time % oneDay) / oneHour);
5567
const minsLeft = Math.floor((time % oneHour) / oneMinute);
5668
const secsLeft = Math.floor((time % oneMinute) / 1000);
5769

5870
const values = [daysLeft, hoursLeft, minsLeft, secsLeft];
59-
60-
deadlineFormats.forEach((text, index) => {
61-
text.innerHTML = format(values[index]);
62-
})
63-
function format(item) {
71+
format = (item) => {
6472
if (item < 10) {
6573
return (item = `0${item}`);
6674
}
6775
return item;
6876
}
69-
77+
deadlineFormats.forEach((text, index) => {
78+
text.innerHTML = format(values[index]);
79+
})
7080
if (time < 0) {
7181
clearInterval(countdown);
72-
deadline.innerHTML = `<h4 class="expired">sorry, this giveaway has expired!</h4>`;
82+
deadline.innerHTML = `<h4 class="expired">Presentation already given!</h4>`;
7383
}
74-
}
75-
let countdown = setInterval(getRemaindingTime, 1000);
76-
getRemaindingTime();
84+
}, 1000);
7785

7886
/* Learnings:
7987
1. date() and its methods
80-
2. SetInterval() method
88+
2. SetInterval() & clearInterval() method
8189
*/

countdown-timer/index.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</article>
1616
<article class="gift-info">
1717
<h3>TDD Presentation Countdown</h3>
18-
<h4 class="presentation">PPT begins on Monday, 03 July, 2020 11:00am</h4>
18+
<h4 class="presentation">PPT begins.</h4>
1919
<p>Topics to prepare for the ppt:</p>
2020
<ol>
2121
<li>What is TDD?</li>
@@ -26,16 +26,16 @@ <h4 class="presentation">PPT begins on Monday, 03 July, 2020 11:00am</h4>
2626
</ol>
2727
<div class="deadline">
2828
<div class="deadline-format">
29-
<div><h4 class="days">34</h4><span>days</span></div>
29+
<div><h4 class="days">00</h4><span>days</span></div>
3030
</div>
3131
<div class="deadline-format">
32-
<div><h4 class="hours">2</h4><span>hours</span></div>
32+
<div><h4 class="hours">00</h4><span>hours</span></div>
3333
</div>
3434
<div class="deadline-format">
35-
<div><h4 class="mins">45</h4><span>mins</span></div>
35+
<div><h4 class="mins">00</h4><span>mins</span></div>
3636
</div>
3737
<div class="deadline-format">
38-
<div><h4 class="secs">45</h4><span>secs</span></div>
38+
<div><h4 class="secs">00</h4><span>secs</span></div>
3939
</div>
4040
</div>
4141
</article>

0 commit comments

Comments
 (0)