In this tutorial we’ll see how to create a countdown clock using JavaScript.
We’ll make the badge in following 3 steps.
1. Make a HTML file and write all markups and script
2. Make a CSS file and define all styling in it
3. Make a javaScript file and define all time code in it
STEP 1. Make a HTML file and write all markups and script
First we make a HTML file and saved it as index.html. This file contains all HTML code for countdown.
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
|
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Countdown Clock Using JavaScript</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Clock Countdown</h1>
<div id="clock_div">
<div>
<span class="day"></span>
<div class="smalltext">Days</div>
</div>
<div>
<span class="hour"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minute"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="second"></span>
<div class="smalltext">Seconds</div>
</div>
</div>
<script src="countdown.js"></script>
</body>
</html>
|
STEP 2.Make a CSS file and define all styling
In this step, we create a CSS file and named it style.css which contains our CSS for countdown.
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
|
body{
text-align: center;
background: #45B39D ;
font-family: calibri;
font-weight: 100;
}
h1{
color: #fff;
font-family:calibri;
font-weight: 100;
font-size: 40px;
margin: 40px 0px 20px;
}
#clock_div{
font-family: calibri;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
font-size: 30px;
}
#clock_div > div{
padding: 10px;
border-radius: 32px;
background: #138D75;
display: inline-block;
}
#clock_div div > span{
padding: 15px;
border-radius: 35px;
background: #0E6655;
display: inline-block;
}
.smalltext{
padding-top: 5px;
font-size: 11px;
}
|
STEP 3. Make a javaScript file and define all time code
This is very important part of this tutorial, in this step we first create a jaavscript file and named it coutdown.js. Then we write our .js code in it.
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
|
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.day');
var hoursSpan = clock.querySelector('.hour');
var minutesSpan = clock.querySelector('.minute');
var secondsSpan = clock.querySelector('.second');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 11 * 24 * 60 * 60 * 1000);
initializeClock('clock_div', deadline);
|
You can see that we use
Math.floor() function it. It is used to representing the largest integer less than or equal to the specified number.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.