Skip to content

Instantly share code, notes, and snippets.

@sudarshann
Created March 11, 2021 07:34
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 sudarshann/1cc651090b0866948ddf3812acfcf991 to your computer and use it in GitHub Desktop.
Save sudarshann/1cc651090b0866948ddf3812acfcf991 to your computer and use it in GitHub Desktop.
Plain JS countdown timer. Read the time from that particular element and replaces with a count down in that same element
jQuery(document).ready(function(){
if(jQuery(".countdown h3 span") === 0 ){
return;
}
jQuery(".countdown h3 span").each(function(){
// Set the date we're counting down to
var element = jQuery(this);
var countDownDate = new Date(jQuery(this).text()).getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
element.text(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
element.text();
element.text("EXPIRED");
}
}, 1000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment