Created
April 14, 2016 21:22
-
-
Save delphinpro/b65558ed73b24da573dc7812de8de016 to your computer and use it in GitHub Desktop.
Counter remaining time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="counter"> | |
<div class="counter-item"> | |
<div class="counter-digits days"></div> | |
<div class="counter-label">дней</div> | |
</div> | |
<div class="counter-item"> | |
<div class="counter-digits hours"></div> | |
<div class="counter-label">часов</div> | |
</div> | |
<div class="counter-item"> | |
<div class="counter-digits minutes"></div> | |
<div class="counter-label">минут</div> | |
</div> | |
<div class="counter-item"> | |
<div class="counter-digits seconds"></div> | |
<div class="counter-label">секунд</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
"use strict"; | |
function pluralize($n, $forms) { | |
return $n % 10 == 1 && $n % 100 != 11 ? $forms[0] : ( $n % 10 >= 2 && $n % 10 <= 4 && ( $n % 100 < 10 || $n % 100 >= 20 ) ? $forms[1] : $forms[2] ); | |
} | |
var xDate = new Date(2016, 2, 20, 23, 59, 59, 999); // 2016 год, март, 20 число, 23 часа, 59 минут, 59 секунд | |
var offset = (-60 * 3 * 60 * 1000) - (xDate.getTimezoneOffset() * 60 * 1000); | |
var getTimeLeft = function () { | |
var delta = xDate - (new Date()) + offset; | |
if (delta < 0) { | |
return {d: 0, h: 0, m: 0, s: 0}; | |
} | |
var timeLeft = new Date(delta); | |
var time = { | |
d: timeLeft.getUTCDate() - 1, | |
h: timeLeft.getUTCHours(), | |
m: timeLeft.getUTCMinutes(), | |
s: timeLeft.getUTCSeconds() | |
}; | |
return time; | |
}; | |
$('.counter').each(function () { | |
var $counter = $(this), | |
$d = $('.days', $counter), | |
$h = $('.hours', $counter), | |
$m = $('.minutes', $counter), | |
$s = $('.seconds', $counter); | |
setInterval(function () { | |
var time = getTimeLeft(); | |
$d.html(time.d); | |
$d.next().text(pluralize(time.d, ['день', 'дня', 'дней'])); | |
$h.html(time.h); | |
$h.next().text(pluralize(time.h, ['час', 'часа', 'часов'])); | |
$m.html(time.m < 10 ? '0' + time.m : time.m); | |
$m.next().text(pluralize(time.m, ['минута', 'минуты', 'минут'])); | |
$s.html(time.s < 10 ? '0' + time.s : time.s); | |
$s.next().text(pluralize(time.s, ['секунда', 'секунды', 'секунд'])); | |
}, 200); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment