Skip to content

Commit 8ef48e6

Browse files
committed
countdown logic
1 parent b322c93 commit 8ef48e6

File tree

9 files changed

+132
-30
lines changed

9 files changed

+132
-30
lines changed

blocks/countdown.css

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/countdown.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/countdown.scss

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,20 @@
33
display: grid;
44
place-items: center;
55

6-
&__inner {
7-
}
8-
96
&__title {
107
margin-bottom: 30px;
118
}
12-
13-
&__counter {
14-
}
159
}
1610

1711
.countdown {
1812
padding: 60px;
1913
&__title {
20-
background: white;
21-
padding: 10px;
22-
border-radius: 10px;
14+
text-align: center;
15+
padding: 20px 30px;
2316
margin-bottom: 40px;
17+
border-radius: 10px;
18+
background: white;
2419
box-shadow: -11px -11px 20px rgba(255, 255, 255, 0.272044), -8px -40px 22px rgba(246, 251, 255, 0.384288), -8px 0px 8px rgba(244, 248, 251, 0.50254), 19px 21px 50px rgba(176, 195, 210, 0.727846);
25-
2620
}
2721

2822
&__deadline {
@@ -31,6 +25,15 @@
3125
padding-bottom: 10px;
3226
}
3327

28+
&__over {
29+
padding: 20px 30px;
30+
border-radius: 15px;
31+
background-color: white;
32+
width: 100%;
33+
text-align: center;
34+
box-shadow: -11px -11px 20px rgba(255, 255, 255, 0.272044), -8px -40px 22px rgba(246, 251, 255, 0.384288), -8px 0px 8px rgba(244, 248, 251, 0.50254), 19px 21px 50px rgba(176, 195, 210, 0.727846);
35+
}
36+
3437
&__format {
3538
border-radius: 15px;
3639
background-color: white;
@@ -42,11 +45,4 @@
4245
text-align: center;
4346
box-shadow: -11px -11px 20px rgba(255, 255, 255, 0.272044), -8px -40px 22px rgba(246, 251, 255, 0.384288), -8px 0px 8px rgba(244, 248, 251, 0.50254), 19px 21px 50px rgba(176, 195, 210, 0.727846);
4447
}
45-
46-
&__num {
47-
}
48-
49-
&__time-unit {
50-
}
51-
5248
}

blocks/counter.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/counter.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/counter.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
}
44

55
.counter {
6+
67
background-color: white;
78
border-radius: 30px;
89
padding: 30px;
10+
margin: 60px;
11+
box-shadow: -11px -11px 20px rgba(255, 255, 255, 0.272044), -8px -40px 22px rgba(246, 251, 255, 0.384288), -8px 0px 8px rgba(244, 248, 251, 0.50254), 19px 21px 50px rgba(176, 195, 210, 0.727846);
912
&__output {
1013
text-align: center;
1114
font-size: 72px;

index.html

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ <h1 class="main__title">Small JavaScript Projects</h1>
2121
</a>
2222
<a href="pages/counter.html" class="cards-block__title">Counter</a>
2323
<p class="cards-block__subtitle">Simple counter.
24-
User can decrement, reset and increment counter.</p>
24+
The user can decrement, reset and increment counter
25+
.</p>
2526
</div>
2627

2728
<div class="cards-block__item">
2829
<a href="pages/counter.html" class="cards-block__image">
2930
<img src="images/main-page/02.png" alt="Reviews">
3031
</a>
3132
<a href="pages/counter.html" class="cards-block__title">Reviews</a>
32-
<p class="cards-block__subtitle">Reviews block. User
33+
<p class="cards-block__subtitle">Reviews block. The user
3334
can scroll horizontally different reviews or get
3435
random content.
3536
</p>
@@ -41,7 +42,7 @@ <h1 class="main__title">Small JavaScript Projects</h1>
4142
</a>
4243
<a href="pages/counter.html"
4344
class="cards-block__title">Sidebar</a>
44-
<p class="cards-block__subtitle">Side menu. User can
45+
<p class="cards-block__subtitle">Side menu. The user can
4546
open and close it at any time.
4647
</p>
4748
</div>
@@ -85,12 +86,14 @@ <h1 class="main__title">Small JavaScript Projects</h1>
8586
</div>
8687

8788
<div class="cards-block__item">
88-
<a href="pages/counter.html" class="cards-block__image">
89-
<img src="images/main-page/07.png" alt="Reviews">
89+
<a href="pages/countdown.html" class="cards-block__image">
90+
<img src="images/main-page/07.png" alt="Countdown">
9091
</a>
9192
<a href="pages/counter.html"
9293
class="cards-block__title">Countdown</a>
93-
<p class="cards-block__subtitle">...</p>
94+
<p class="cards-block__subtitle">Countdown. The user
95+
can find out how much time is left for a certain
96+
date.</p>
9497
</div>
9598

9699
<div class="cards-block__item">

pages/countdown.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
<h1 class="main__title">Countdown</h1>
1717

1818
<div class="main__counter countdown">
19-
<h2 class="countdown__title">Something will happen on
20-
Monday 6th May at 12:30</h2>
19+
<h2 class="countdown__title">Something will happen on <br>
20+
<span>Monday 6th May at 12:30</span>
21+
</h2>
2122
<div class="countdown__deadline">
2223
<div class="countdown__format">
2324
<div class="">

scripts/countdown.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const months = [
2+
"January",
3+
"February",
4+
"March",
5+
"April",
6+
"May",
7+
"June",
8+
"July",
9+
"August",
10+
"September",
11+
"October",
12+
"November",
13+
"December",
14+
]
15+
16+
const weekdays = [
17+
"Sunday",
18+
"Monday",
19+
"Tuesday",
20+
"Wednesday",
21+
"Thursday",
22+
"Friday",
23+
"Saturday",
24+
]
25+
26+
const endDateSpan = document.querySelector('.countdown__title span')
27+
const deadline = document.querySelector('.countdown__deadline')
28+
const items = document.querySelectorAll('.countdown__format h1')
29+
console.log(items)
30+
31+
let futureDate = new Date(2022,11,21, 21, 21,21);
32+
const year = futureDate.getFullYear()
33+
const hours = futureDate.getHours()
34+
const minutes = futureDate.getMinutes()
35+
let month = futureDate.getMonth()
36+
month = months[month]
37+
const day = futureDate.getDate()
38+
let weekday = weekdays[futureDate.getDay()]
39+
40+
endDateSpan.innerText = `${weekday}, ${day} of ${month} ${year} ${hours}:${minutes}`
41+
42+
//
43+
const futureTime = futureDate.getTime()
44+
45+
function getRemainingTime() {
46+
const currentTime = new Date().getTime()
47+
let time = futureTime - currentTime
48+
49+
const oneDay = 24 * 60 * 60 * 1000
50+
const oneHour = 60 * 60 * 1000
51+
const oneMinute = 60 * 1000
52+
53+
let days = time / oneDay;
54+
days = Math.floor(days);
55+
let hours = Math.floor((time % oneDay) / oneHour);
56+
let minutes = Math.floor((time % oneHour) / oneMinute);
57+
let seconds = Math.floor((time % oneMinute) / 1000);
58+
59+
const remainingTimeValues = [
60+
Math.floor(days),
61+
Math.floor(hours),
62+
Math.floor(minutes),
63+
Math.floor(seconds)
64+
]
65+
66+
function format(item) {
67+
if (item < 10) {
68+
item = `0${item}`
69+
}
70+
return item
71+
72+
}
73+
74+
items.forEach( (item, index) => {
75+
item.innerHTML = format(remainingTimeValues[index])
76+
})
77+
78+
console.log(time)
79+
if (time < 0) {
80+
clearInterval(countdown)
81+
deadline.innerHTML = `<h3 class="countdown__over">
82+
Congratulations the countdown is over!!!</h3>`
83+
}
84+
}
85+
86+
let countdown = setInterval(getRemainingTime, 1000)
87+
getRemainingTime()

0 commit comments

Comments
 (0)