Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 29, 2023 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4edec89ad159349616df2d56f77ac7cc to your computer and use it in GitHub Desktop.
Save code-boxx/4edec89ad159349616df2d56f77ac7cc to your computer and use it in GitHub Desktop.
Javascript Countdown Timer

JAVASCRIPT COUNTDOWN TIMER

https://code-boxx.com/simple-javascript-countdown-timer/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Countdown Timer</title>
<!-- (A) LOAD LIBRARY & THEME -->
<link href="countdown-dark.css" rel="stylesheet">
<script src="countdown.js"></script>
</head>
<body>
<!-- (B) GENERATE COUNTDOWN TIMER HERE -->
<div id="demoA"></div>
<!-- (C) ATTACH COUNTDOWN TIMER -->
<script>
counter.attach({
target : document.getElementById("demoA"),
remain : 5400 // 90 mins = 5400 secs
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Countdown Timer</title>
<!-- (A) LOAD LIBRARY & THEME -->
<link href="countdown-dark.css" rel="stylesheet">
<script src="countdown.js"></script>
</head>
<body>
<!-- (B) GENERATE COUNTDOWN TIMER HERE -->
<div id="demoB"></div>
<!-- (C) ATTACH COUNTDOWN TIMER -->
<script>
counter.attach({
target : document.getElementById("demoB"),
remain : 65,
after : () => console.log("TIMER HAS ENDED!")
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Countdown Timer</title>
<!-- (A) LOAD LIBRARY & THEME -->
<link href="countdown-light.css" rel="stylesheet">
<script src="countdown.js"></script>
</head>
<body>
<!-- (B) GENERATE COUNTDOWN TIMER HERE -->
<div id="demoC"></div>
<!-- (C) ATTACH COUNTDOWN TIMER -->
<script>
// (C1) FOR THIS EXAMPLE, WE TAKE 2 DAYS FROM NOW
// IN YOUR PROJECT THIS IS NEW DATE("YOUR TARGET DATE/TIME")
var till = new Date(Date.now() + 172800000);
// (C2) COUNTDOWN
counter.attach({
target : document.getElementById("demoC"),
remain : counter.toSecs(till)
});
</script>
</body>
</html>
/* (A) WRAPPER */
.countdown * {
font-family: Impact, sans-serif;
box-sizing: border-box;
text-align: center;
}
.countdown {
display: flex;
margin: 0 auto;
max-width: 450px;
background: #000;
}
/* (B) DAY/HR/MIN/SEC CELLS */
.countdown .cell {
flex-grow: 1;
flex-basis: 0;
padding: 10px;
}
.countdown .digits {
font-size: 24px;
color: #000;
background: #fff;
padding: 10px;
border-radius: 5px;
}
.countdown .text {
margin-top: 10px;
color: #ddd;
}
/* (C) IF YOU WANT TO SPECIFICALLY STYLE THE DAY/HR/MIN/SEC
.countdown .days { }
.countdown .hours { }
.countdown .minutes { }
.countdown .seconds { }
*/
/* (A) WRAPPER */
.countdown * {
font-family: Impact, sans-serif;
box-sizing: border-box;
text-align: center;
}
.countdown {
display: flex;
margin: 0 auto;
max-width: 450px;
background: #f2f2f2;
}
/* (B) DAY/HR/MIN/SEC CELLS */
.countdown .cell {
flex-grow: 1;
flex-basis: 0;
padding: 10px;
}
.countdown .digits {
font-size: 24px;
color: #ff3f6b;
background: #fff;
padding: 10px;
border-radius: 5px;
}
.countdown .text {
margin-top: 10px;
color: #1e1e1e;
}
/* (C) IF YOU WANT TO SPECIFICALLY STYLE THE DAY/HR/MIN/SEC
.countdown .days { }
.countdown .hours { }
.countdown .minutes { }
.countdown .seconds { }
*/
var counter = {
// (A) HELPER - CREATE HR/MIN/SEC CELL
// txt : text for the cell (all small letters)
square : txt => {
let cell = document.createElement("div");
cell.className = `cell ${txt}`;
cell.innerHTML = `<div class="digits">0</div><div class="text">${txt}</div>`;
return cell;
},
// (B) INITIALIZE COUNTDOWN TIMER
// target : target html container
// remain : seconds to countdown
// after : function, do this when countdown end (optional)
attach : instance => {
// (B1) GENERATE HTML
instance.target.className = "countdown";
if (instance.remain >= 86400) {
instance.target.appendChild(counter.square("days"));
instance.days = instance.target.querySelector(".days .digits");
}
if (instance.remain >= 3600) {
instance.target.appendChild(counter.square("hours"));
instance.hours = instance.target.querySelector(".hours .digits");
}
if (instance.remain >= 60) {
instance.target.appendChild(counter.square("mins"));
instance.mins = instance.target.querySelector(".mins .digits");
}
instance.target.appendChild(counter.square("secs"));
instance.secs = instance.target.querySelector(".secs .digits");
// (B2) TIMER
instance.timer = setInterval(() => counter.ticker(instance), 1000);
},
// (C) COUNTDOWN TICKER
ticker : instance => {
// (C1) TIMER STOP
instance.remain--;
if (instance.remain<=0) {
clearInterval(instance.timer);
instance.remain = 0;
if (typeof instance.after == "function") { instance.after(); }
}
// (C2) CALCULATE REMAINING DAYS/HOURS/MINS/SECS
// 1 day = 60 secs * 60 mins * 24 hrs = 86400 secs
// 1 hr = 60 secs * 60 mins = 3600 secs
// 1 min = 60 secs
let secs = instance.remain;
let days = Math.floor(secs / 86400);
secs -= days * 86400;
let hours = Math.floor(secs / 3600);
secs -= hours * 3600;
let mins = Math.floor(secs / 60);
secs -= mins * 60;
// (C3) UPDATE HTML
instance.secs.innerHTML = secs;
if (instance.mins !== undefined) { instance.mins.innerHTML = mins; }
if (instance.hours !== undefined) { instance.hours.innerHTML = hours; }
if (instance.days !== undefined) { instance.days.innerHTML = days; }
},
// (D) HELPER - CONVERT DATE/TIME TO REMAINING SECONDS
// till : (date object) countdown to this date/time
toSecs : till => {
till = Math.floor(till / 1000);
let remain = till - Math.floor(Date.now() / 1000);
return remain<0 ? 0 : remain;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment