JSFiddle - React, Tailwind, and code Playground
HTML
<div id="counter">Содержимое контейнера должно быть таймером</div>
<ul class="number">
<li><span>лет</span>11</li><li><span>месяцев</span>7</li><li><span>день</span>21</li><li><span>секунд</span>56</li>
</ul>
JavaScript
function CountUp(initDate, id) {
this.beginDate = new Date(initDate);
this.countainer = document.getElementById(id);
this.numOfDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
this.borrowed = 0;
this.years = 0;
this.months = 0;
this.days = 0;
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
this.localize_rus = {
'years': ['год', 'года', 'лет'],
'months': ['месяц', 'месяца', 'месяцев'],
'days': ['день', 'дня', 'дней'],
'hours': ['час', 'часа', 'часов'],
'minutes': ['минута', 'минуты', 'минут'],
'seconds': ['секунда', 'секунды', 'секунд']
};
this.updateNumOfDays();
this.updateCounter();
}
CountUp.prototype.updateNumOfDays = function() {
var dateNow = new Date();
var currYear = dateNow.getFullYear();
if ((currYear % 4 == 0 && currYear % 100 != 0) || currYear % 400 == 0) {
this.numOfDays[1] = 29;
}
var self = this;
setTimeout(function() {
self.updateNumOfDays();
}, (new Date((currYear + 1), 1, 2) - dateNow));
};
CountUp.prototype.datePartDiff = function(then, now, MAX) {
var diff = now - then - this.borrowed;
this.borrowed = 0;
if (diff > -1) return diff;
this.borrowed = 1;
return (MAX + diff);
};
CountUp.prototype.calculate = function() {
var currDate = new Date();
var prevDate = this.beginDate;
this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(), 0);
};
CountUp.prototype.addLeadingZero = function(value)...