Skip to content

Instantly share code, notes, and snippets.

@mir4a
Last active December 15, 2015 05:49
Show Gist options
  • Save mir4a/5211493 to your computer and use it in GitHub Desktop.
Save mir4a/5211493 to your computer and use it in GitHub Desktop.
Countdown timer
//<editor-fold desc="Таймер обратного отсчета">
var t_data = document.getElementById('tender_data'); // get element with data-set
var timer_end_date = t_data.getAttribute('data-timer-end'); // get data-set timer-end
var timerHour = $('.b-timer_wrap > .i-hour'); // where to write hours value
var timerMin = $('.b-timer_wrap > .i-minute'); // where to write minutes value
var timerSec = $('.b-timer_wrap > .i-sec'); // where to write seconds value
function zero(number) {
return (number < 10 ? '0' : '') + number;
}
var timer = setInterval(function(){updateWCTime()}, 100 );
function updateWCTime() {
now = new Date();
kickoff = Date.parse(timer_end_date);
diff = kickoff - now;
if (diff > 0) {
days = Math.floor( diff / (1000*60*60*24) );
hours = Math.floor( diff / (1000*60*60) );
mins = Math.floor( diff / (1000*60) );
secs = Math.floor( diff / 1000 );
dd = days;
// hh = hours - days * 24;
hh = hours;
mm = mins - hours * 60;
ss = secs - mins * 60;
timerHour.text(zero(hh));
timerMin.text(zero(mm));
timerSec.text(zero(ss));
} else { // What to do if countdown reached it's limit (in my case I'll write 00 for HH:MM:SS)
timerHour.text('00');
timerMin.text('00');
timerSec.text('00');
clrInt(timer);
return false;
}
}
function clrInt(int) {
clearInterval(int); // прекратить функцию setInterval
}
//</editor-fold>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment