Skip to content

Instantly share code, notes, and snippets.

@MSerj
Last active April 11, 2018 11:46
Show Gist options
  • Save MSerj/d99b7a2196a46ef1eb45e1da0d965661 to your computer and use it in GitHub Desktop.
Save MSerj/d99b7a2196a46ef1eb45e1da0d965661 to your computer and use it in GitHub Desktop.
Timer countdown
<div class="countdown-block">
<div class="text">Don't miss it! Token sale ends in:</div>
<div class="countdown-wrap">
<div class="countdown" id="countdown">
<span id="days">30</span>
<span id="hours">21</span>
<span id="minutes">15</span>
<span id="seconds">00</span>
</div>
<div class="countdown-text">
<span class="day">Days</span>
<span class="hour">Hours</span>
<span class="min">Minutes</span>
<span class="sec">Seconds</span>
</div>
</div>
</div>
/////////////////////////countdown 24 hours NO cookie//////////////////////////////
// set the date we're counting down to
const target_date = new Date();
target_date.setHours(target_date.getHours() + 23);
target_date.setMinutes(target_date.getMinutes() + 59);
target_date.setSeconds(target_date.getSeconds() + 59);
// variables for time units
let days, hours, minutes, seconds;
// update the tag with id "countdown" every 1 second
function start_timer() {
// find the amount of "seconds" between now and target
const current_date = new Date().getTime();
let seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
if (days < 10) days = "0" + days;
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
days = "<span id='days'>"+days+"</span>";
hours = "<span id='hours'>"+hours+"</span>";
minutes = "<span id='minutes'>"+minutes+"</span>";
seconds = "<span id='seconds'>"+seconds+"</span>";
// format countdown string + set tag value
$('#countdown').html(days + " : " + hours + " : " + minutes + " : " + seconds );
}
//cookie to timer
if (!Cookies.get('timer')) {
Cookies.set('timer', target_date, { expires: 1 });
}
const cookie = Cookies.get('timer');
if (new Date(cookie) > new Date()) {
target_date = new Date(cookie);
}
//end if cookie
start_timer(); //forced run timer
setInterval(start_timer, 1000); //set interval to timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment