Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active May 29, 2016 04:12
Show Gist options
  • Save nishinoshake/39a338377a01d52e72af to your computer and use it in GitHub Desktop.
Save nishinoshake/39a338377a01d52e72af to your computer and use it in GitHub Desktop.
カウントダウンをクラスの切り替えで
$(function() {
var limit = new Date("Feb 1,2016 00:00:00"),
$times = [
$('#date10'), $('#date01'),
$('#hour10'), $('#hour01'),
$('#minute10'), $('#minute01'),
$('#second10'), $('#second01')
];
var updateTime = function(timeArray) {
if ( $times.length !== timeArray.length ) return;
for ( var i = 0, length = timeArray.length; i < length; i++ ) {
if ( ! $times[i].hasClass('num' + timeArray[i]) ) {
$times[i].removeClass(function(index, className) {
return (className.match(/num\d/) || []).join(' ');
}).addClass('num' + timeArray[i]);
}
}
};
setInterval(function() {
var now = new Date(),
left = limit - now,
day = 24 * 60 * 60 * 1000,
d = Math.floor(left / day),
h = Math.floor((left % day) / (60 * 60 * 1000)),
m = Math.floor((left % day) / (60 * 1000)) % 60,
s = Math.floor((left % day) / 1000) % 60;
updateTime([
Math.floor(d / 10), d % 10,
Math.floor(h / 10), h % 10,
Math.floor(m / 10), m % 10,
Math.floor(s / 10), s % 10,
]);
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment