LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pure Vanilla JavaScript CountDown Timer With Timer Bar

Pure Vanilla JavaScript CountDown Timer With Timer Bar

Pure-Vanilla-JavaScript-CountDown-Timer-With-Timer-Bar Sometimes, you’re going to need to build a JavaScript countdown clock. You may have an event, a sale, a promotion, or a game. You can build a clock in raw JavaScript rather than reaching for the nearest plugin. While there are many great clock plugins, here are the benefits you’ll get from using raw JavaScript: Your code will be lightweight because it will have zero dependencies.

  • Your website will perform better.
  • You won’t need to load external scripts and stylesheets.
  • You’ll have more control.
  • You will have built the clock to behave exactly the way you want it to (rather than trying to bend a plugin to your will).

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more straightforward to make one than you might think and only requires the trifecta of HTML, CSS, and JavaScript. Let’s make one together!

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Pure Vanilla JavaScript Custom Form Validation In HTML Forms

Features:

  1. Light Weight.
  2. Pure Vanilla JavaScript Code.
  3. No External Files.
  4. Work With Any Format Of Date.
  5. Fully Customizable.

How To Get Pure Vanilla JavaScript CountDown Timer With Timer Bar?

There are a few easy and understandable steps to achieve your desired functionality using pure PHP that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
.exe_loader_bar {
display: block;
height: 50px;
margin-bottom: 30px;
width: 100%;
border: 2px solid #DD0000;
background: #DDD;
position: relative;
font-weight: bold;
}
.exe_loader_bar > div {
background: #DD0000;
height: 50px;
position: absolute;
top: 0;
left: 0;
}
.exe_loader_bar > span {
position: absolute;
top: 50px;
left: 0;
line-height: 0;
}
</style>

HTML:

<div id="exe_loader_bar_1" class="exe_loader_bar"><div></div><span></span></div>
<div id="exe_loader_bar_2" class="exe_loader_bar"><div></div><span></span></div>
<div id="exe_loader_bar_3" class="exe_loader_bar"><div></div><span></span></div>
<div id="exe_loader_bar_4" class="exe_loader_bar"><div></div><span></span></div>
<div id="exe_loader_bar_5" class="exe_loader_bar"><div></div><span></span></div>

JavaScript:

<script type="text/javascript">
/* Call Your Timers Here */
exe_runTimerDownCounter( "exe_loader_bar_1", "2010-11-21 10:30:59", "2020-11-21 16:36:59"); // Call This When You Need A Timer
exe_runTimerDownCounter( "exe_loader_bar_2", "2020-11-20 10:30:59", "2030-11-22 11:36:59"); // Call This When You Need A Timer
exe_runTimerDownCounter( "exe_loader_bar_3", "2030-11-20 10:30:59", "2040-11-21 11:36:59"); // Call This When You Need A Timer
exe_runTimerDownCounter( "exe_loader_bar_4", "2020-11-22 10:30:59", "2050-11-23 11:36:59"); // Call This When You Need A Timer
exe_runTimerDownCounter( "exe_loader_bar_5", "2050-11-20 10:30:59", "2060-11-24 11:36:59"); // Call This When You Need A Timer
/*************************/

/**
* Function: Show A Real-TIme Running Counter Between Two Dates With Bar And Text
* @param {String, String, String} ==>> ("exe_loader_bar_1", "2022-11-21 10:30:59", "2022-11-21 11:30:59")
* @return {NULL}
* Developed by: Muhammad Hassan
*/
function exe_runTimerDownCounter(counter_id, startDateTime, endDateTime){
var secCounter = 0; 
var timerBar = document.querySelector('#'+counter_id+' div');
var timerText = document.querySelector('#'+counter_id+' span');
var totalDiff = dateDiff( endDateTime, startDateTime );
setInterval(function () {
var now = new Date();
secCounter++;
if(Date.parse( startDateTime ) - Date.now() > 0 ){
// Check If The Timer DateTime Is Not Started Yet
timerBar.style.width = '0%';
timerText.innerHTML = '<p style="color:#0055CC;">Timer Will Start From: '+startDateTime+'</p>';
} else if(Date.parse( endDateTime ) - Date.now() < 0 ){
// Check If The Timer DateTime Is Expired
timerBar.style.width = '0%';
timerText.innerHTML = '<p style="color:#DD0000;">Timer Is Expired On: '+endDateTime+'</p>';
} else {
// Current DateTIme Is Within Upper Timer Range
var currentDiff = dateDiff( endDateTime, now );
var remainingPercentage = (currentDiff.diff / totalDiff.diff) * 100;
var passedPercentage = remainingPercentage - 100;
var passedPercentage = passedPercentage >= 100 ? 100 : passedPercentage;
timerBar.style.width = Math.abs(passedPercentage)+'%';
timerText.innerHTML = '<p style="color:#009900;">'+currentDiff.d+' Days, '+currentDiff.h+' Hours, '+currentDiff.m+' Minutes & '+currentDiff.s+' Seconds Remaining Only</p>';
}

}, 1000);
}
/**
* Function: Find the Difference in DateTime
* @param {String, String} ==>> ("2022-11-31 23:59:59", "2022-11-01 23:59:59")
* @return {Object} ==>> { diff: 131400000, d: 2, h: 13, m: 30, s: 0, ms: 0 }
* Developed by: Muhammad Hassan
*/
function dateDiff( str1, str2 ) { //return { diff: 131400000, d: 2, h: 13, m: 30, s: 0, ms: 0 }
var diff = Date.parse( str1 ) - Date.parse( str2 );
return isNaN( diff ) ? NaN : {
diff : diff,
ms : Math.round( diff % 1000 ),
s : Math.round( diff / 1000 % 60 ),
m : Math.round( diff / 60000 % 60 ),
h : Math.round( diff / 3600000 % 24 ),
d : Math.round( diff / 86400000 )
};
}
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
How To Customize Radio Buttons And Checkboxe Buttons With Pure CSS?

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *