Contador regressivo feito em Javascript
by Ivan Ferrer
HTML
A data setada é "20 October 2015 09:00:00":
<div id="countdown">
<p class="days">00</p>
<p class="timeRefDays">dias</p>
<p class="hours">00</p>
<p class="timeRefHours">horas</p>
<p class="minutes">00</p>
<p class="timeRefMinutes">minutos</p>
<p class="seconds">00</p>
<p class="timeRefSeconds">segundos</p>
</div>
CSS
#countdown p {
display: inline-block;
padding: 5px;
background: #eee;
margin: 0 0 20px;
}
JavaScript
(function (e) {
e.fn.countdown = function (t, n) {
function i() {
eventDate = Date.parse(r.date) / 1e3;
currentDate = Math.floor(e.now() / 1e3);
if (eventDate <= currentDate) {
n.call(this);
clearInterval(interval)
}
seconds = eventDate - currentDate;
days = Math.floor(seconds / 86400);
seconds -= days * 60 * 60 * 24;
hours = Math.floor(seconds / 3600);
seconds -= hours * 60 * 60;
minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
days == 1 ? thisEl.find(".timeRefDays").text("dias") : thisEl.find(".timeRefDays").text("dias");
hours == 1 ? thisEl.find(".timeRefHours").text("horas") : thisEl.find(".timeRefHours").text("horas");
minutes == 1 ? thisEl.find(".timeRefMinutes").text("minutos") : thisEl.find(".timeRefMinutes").text("minutos");
seconds == 1 ? thisEl.find(".timeRefSeconds").text("segundos") : thisEl.find(".timeRefSeconds").text("segundos");
if (r["format"] == "on") {
days = String(days).length >= 2 ? days : "0" + days;
hours = String(hours).length >= 2 ? hours : "0" + hours;
minutes = String(minutes).length >= 2 ? minutes : "0" + minutes;
seconds = String(seconds).length >= 2 ? seconds : "0" + seconds
}
if (!isNaN(eventDate)) {
thisEl.find(".days").text(days);
thisEl.find(".hours").text(hours);
thisEl.find(".minutes").text(minutes);
thisEl.find(".seconds").text(seconds)
} else {
alert("Invalid date. Example: 30 Tuesday 2013 15:50:00");
clearInterval(interval)
}
}
var thisEl = e(this);
var r = {
date: null,
format: null
};
t && e.extend(r, t);
i();
interval = setInterval(i, 1e3)
}
})(jQuery);
$(document).ready(function () {
function e() {
var e = new Date;
e.setDate(e.getDate() + 60);
dd = e.getDate();
mm = e.getMonth() + 1;
y = e.getFullYear();
futureFormattedDate = mm + "/" + dd + "/" + y;
return futureFormattedDate
...