MediaWiki:Countdown.js

/* FINNER/Countdown START */ function Countdown(seedDate, loopTime, loopUnit, delayTime, delayUnit, dateFormat, dateLabels, separators) { const oneYear = 31536000000, oneMonth = oneYear / 12, oneDay    = oneYear / 365; const oneHour = 3600000,    oneMinute = 60000,        oneSecond = 1000; var count = document.getElementsByClassName("customcountdown"), s = 0, r = 0, t = 0, u = 0; for (var k = 0; k < count.length; k++) {   var now = new Date; var i = 0, loopConv = 0, delayConv = 0; var nextDate = new Date(seedDate[k].innerHTML); if (isNaN(delayTime[k].innerHTML)) delayTime[k].innerHTML = 0; switch (loopUnit[k].innerHTML) { case 'Y': loopConv = loopTime[k].innerHTML * oneYear; break; case 'M': loopConv = loopTime[k].innerHTML * oneMonth; break; case 'D': loopConv = loopTime[k].innerHTML * oneDay; break; case 'h': loopConv = loopTime[k].innerHTML * oneHour; break; case 'm': loopConv = loopTime[k].innerHTML * oneMinute; break; case 's': loopConv = loopTime[k].innerHTML * oneSecond; break; }   switch (delayUnit[k].innerHTML) { case 'Y': delayConv = delayTime[k].innerHTML * oneYear; break; case 'M': delayConv = delayTime[k].innerHTML * oneMonth; break; case 'D': delayConv = delayTime[k].innerHTML * oneDay; break; case 'h': delayConv = delayTime[k].innerHTML * oneHour; break; case 'm': delayConv = delayTime[k].innerHTML * oneMinute; break; case 's': delayConv = delayTime[k].innerHTML * oneSecond; break; }   var nextDate2 = new Date(nextDate.getTime + delayConv); while (nextDate.getTime <= now.getTime) { nextDate.setTime(nextDate.getTime + loopConv); i += 1; }   while (nextDate2.getTime <= now.getTime) { nextDate2.setTime(nextDate2.getTime + loopConv); }   var dst_offset = 0, dst_offset2 = 0; if (document.getElementById('dst_' + k).innerHTML === '') { dst_offset = (now.getTimezoneOffset - nextDate.getTimezoneOffset)  * 60 * 1000; dst_offset2 = (now.getTimezoneOffset - nextDate2.getTimezoneOffset) * 60 * 1000; }   var diff  = (nextDate.getTime  - now.getTime) + dst_offset; var diff2 = (nextDate2.getTime - now.getTime) + dst_offset2; var yearsleft   = Math.floor(diff  / oneYear); var yearsleft2  = Math.floor(diff2 / oneYear); var monthsleft  = Math.floor(diff  / oneMonth); var monthsleft2 = Math.floor(diff2 / oneMonth); var daysleft    = Math.floor(diff  / oneDay); var daysleft2   = Math.floor(diff2 / oneDay); var hoursleft   = Math.floor(diff  / oneHour); var hoursleft2  = Math.floor(diff2 / oneHour); var minutesleft = Math.floor(diff  / oneMinute); var minutesleft2 = Math.floor(diff2 / oneMinute); var secondsleft = Math.floor(diff  / oneSecond); var secondsleft2 = Math.floor(diff2 / oneSecond); var Y_Count = 0, M_Count = 0, D_Count = 0, h_Count = 0, m_Count = 0, s_Count = 0; for (var position = 0; position < dateFormat[k].innerHTML.length; position++) { switch (dateFormat[k].innerHTML.charAt(position)) { case 'Y': Y_Count += 1; break; case 'M': M_Count += 1; break; case 'D': D_Count += 1; break; case 'h': h_Count += 1; break; case 'm': m_Count += 1; break; case 's': s_Count += 1; break; }   }    if (Y_Count === 0) {yearsleft   = 0; yearsleft2   = 0;} if (M_Count === 0) {monthsleft = 0; monthsleft2  = 0;} else { monthsleft = (monthsleft *oneMonth - yearsleft *oneYear) / oneMonth; monthsleft2 = (monthsleft2*oneMonth - yearsleft2*oneYear) / oneMonth; }   if (D_Count === 0) {daysleft    = 0; daysleft2    = 0;} else { daysleft = (daysleft *oneDay - yearsleft *oneYear - monthsleft *oneMonth) / oneDay; daysleft2 = (daysleft2*oneDay - yearsleft2*oneYear - monthsleft2*oneMonth) / oneDay; }   if (h_Count === 0) {hoursleft   = 0; hoursleft2   = 0;} else { hoursleft = (hoursleft *oneHour - yearsleft *oneYear - monthsleft *oneMonth - daysleft *oneDay) / oneHour; hoursleft2 = (hoursleft2*oneHour - yearsleft2*oneYear - monthsleft2*oneMonth - daysleft2*oneDay) / oneHour; }   if (m_Count === 0) {minutesleft = 0; minutesleft2 = 0;} else { minutesleft = (minutesleft *oneMinute - yearsleft *oneYear - monthsleft *oneMonth - daysleft *oneDay - hoursleft *oneHour) / oneMinute; minutesleft2 = (minutesleft2*oneMinute - yearsleft2*oneYear - monthsleft2*oneMonth - daysleft2*oneDay - hoursleft2*oneHour) / oneMinute; }   if (s_Count === 0) {secondsleft = 0; secondsleft2 = 0;} else { secondsleft = (secondsleft *oneSecond - yearsleft *oneYear - monthsleft *oneMonth - daysleft *oneDay - hoursleft *oneHour - minutesleft *oneMinute) / oneSecond; secondsleft2 = (secondsleft2*oneSecond - yearsleft2*oneYear - monthsleft2*oneMonth - daysleft2*oneDay - hoursleft2*oneHour - minutesleft2*oneMinute) / oneSecond; }   var Y_zeros  = , M_zeros  = , D_zeros  = , h_zeros  = , m_zeros  = , s_zeros  = ; var Y_zeros2 = , M_zeros2 = , D_zeros2 = , h_zeros2 = , m_zeros2 = , s_zeros2 = ; for (var j = 1; j < s_Count; j++) { if (secondsleft < Math.pow(10, s_Count - j)) s_zeros  = '0' + s_zeros; if (secondsleft2 < Math.pow(10, s_Count - j)) s_zeros2 = '0' + s_zeros2; }   for (j = 1; j < m_Count; j++) { if (minutesleft < Math.pow(10, m_Count - j)) m_zeros  = '0' + m_zeros; if (minutesleft2 < Math.pow(10, m_Count - j)) m_zeros2 = '0' + m_zeros2; }   for (j = 1; j < h_Count; j++) { if (hoursleft < Math.pow(10, h_Count - j)) h_zeros  = '0' + h_zeros; if (hoursleft2 < Math.pow(10, h_Count - j)) h_zeros2 = '0' + h_zeros2; }   for (j = 1; j < D_Count; j++) { if (daysleft < Math.pow(10, D_Count - j)) D_zeros  = '0' + D_zeros; if (daysleft2 < Math.pow(10, D_Count - j)) D_zeros2 = '0' + D_zeros2; }   for (j = 1; j < M_Count; j++) { if (monthsleft < Math.pow(10, M_Count - j)) M_zeros  = '0' + M_zeros; if (monthsleft2 < Math.pow(10, M_Count - j)) M_zeros2 = '0' + M_zeros2; }   for (j = 1; j < Y_Count; j++) { if (yearsleft < Math.pow(10, Y_Count - j)) Y_zeros  = '0' + Y_zeros; if (yearsleft2 < Math.pow(10, Y_Count - j)) Y_zeros2 = '0' + Y_zeros2; }   var yearunit = , monthunit  = , dayunit    = ''; var hourunit = , minuteunit = , secondunit = ''; if (dateLabels[k].innerHTML === 'full') { yearunit = ' Years'; monthunit = ' Months'; dayunit     = ' Days'; hourunit = ' Hours'; minuteunit = ' Minutes'; secondunit = ' Seconds'; } else if (dateLabels[k].innerHTML === 'single') { yearunit = 'Y'; monthunit = 'M'; dayunit    = 'D'; hourunit = 'h'; minuteunit = 'm'; secondunit = 's'; }   var sep = separators[k].innerHTML; if (separators[k].innerHTML === ) sep = ; var counttext = document.getElementsByClassName("customcountdown")[k].innerHTML; var n = counttext.search(/loopCount/i), m = counttext.search(/countXinterval/i); var o = counttext.search(/loopCount2/i), p = counttext.search(/countXinterval2/i); if ((Math.floor(diff2 / oneSecond) * oneSecond) < delayConv) { document.getElementById('bText_' + k).setAttribute("style","display:none"); document.getElementById('aText_' + k).setAttribute("style","display:none"); document.getElementById('aDelayText_' + k).setAttribute("style","display:visible"); document.getElementById('bDelayText_' + k).setAttribute("style","display:visible"); if (document.getElementById('delayCountDisplay_' + k).innerHTML === '') { if (Y_Count !== 0) $('#years_' + k).html(Y_zeros2  + Math.floor(yearsleft2)   + yearunit   + sep); if (M_Count !== 0) $('#months_' + k).html(M_zeros2 + Math.floor(monthsleft2)  + monthunit  + sep); if (D_Count !== 0) $('#days_' + k).html(D_zeros2   + Math.floor(daysleft2)    + dayunit    + sep); if (h_Count !== 0) $('#hours_' + k).html(h_zeros2  + Math.floor(hoursleft2)   + hourunit   + sep); if (m_Count !== 0) $('#minutes_' + k).html(m_zeros2 + Math.floor(minutesleft2) + minuteunit + sep); if (s_Count !== 0) $('#seconds_' + k).html(s_zeros2 + Math.floor(secondsleft2) + secondunit); } else { $('#years_' + k).html(); $('#months_' + k).html(); $('#days_' + k).html(''); $('#hours_' + k).html(); $('#minutes_' + k).html(); $('#seconds_' + k).html(''); }       if ($('.loopCount').length > 0) { if (n !== -1) document.getElementById('loopCount_' + (k - s)).innerHTML = i - 1; else s += 1; }       if ($('.countXinterval').length > 0) { if (m !== -1) document.getElementById('countXinterval_' + (k - r)).innerHTML = (i - 1) * loopTime[k].innerHTML; else r += 1; }       if ($('.loopCount2').length > 0) { if (o !== -1) document.getElementById('loopCount2_' + (k - t)).innerHTML = i - 1; else t += 1; }       if ($('.countXinterval2').length > 0) { if (p !== -1) document.getElementById('countXinterval2_' + (k - u)).innerHTML = (i - 1) * loopTime[k].innerHTML; else u += 1; }   } else { document.getElementById('bDelayText_' + k).setAttribute("style","display:none"); document.getElementById('aDelayText_' + k).setAttribute("style","display:none"); document.getElementById('aText_' + k).setAttribute("style","display:visible"); document.getElementById('bText_' + k).setAttribute("style","display:visible"); if (Y_Count !== 0) $('#years_' + k).html(Y_zeros  + Math.floor(yearsleft)   + yearunit   + sep); if (M_Count !== 0) $('#months_' + k).html(M_zeros + Math.floor(monthsleft)  + monthunit  + sep); if (D_Count !== 0) $('#days_' + k).html(D_zeros   + Math.floor(daysleft)    + dayunit    + sep); if (h_Count !== 0) $('#hours_' + k).html(h_zeros  + Math.floor(hoursleft)   + hourunit   + sep); if (m_Count !== 0) $('#minutes_' + k).html(m_zeros + Math.floor(minutesleft) + minuteunit + sep); if (s_Count !== 0) $('#seconds_' + k).html(s_zeros + Math.floor(secondsleft) + secondunit); if ($('.loopCount').length > 0) { if (n !== -1) document.getElementById('loopCount_' + (k - s)).innerHTML = i; else s += 1; }       if ($('.countXinterval').length > 0) { if (m !== -1) document.getElementById('countXinterval_' + (k - r)).innerHTML = i * loopTime[k].innerHTML; else r += 1; }       if ($('.loopCount2').length > 0) { if (o !== -1) document.getElementById('loopCount2_' + (k - t)).innerHTML = i; else t += 1; }       if ($('.countXinterval2').length > 0) { if (p !== -1) document.getElementById('countXinterval2_' + (k - u)).innerHTML = i * loopTime[k].innerHTML; else u += 1; }   }    } } var count = document.getElementsByClassName("customcountdown"); var loopCount = 0; if ($('.loopCount').length > 0) { loopCount = document.getElementsByClassName('loopCount'); for (var k = 0; k < loopCount.length; k++) { loopCount[k].id = 'loopCount_' + k;   } } var countXinterval = 0; if ($('.countXinterval').length > 0) { countXinterval = document.getElementsByClassName('countXinterval'); for (k = 0; k < countXinterval.length; k++) { countXinterval[k].id = 'countXinterval_' + k;   } } var loopCount2 = 0; if ($('.loopCount2').length > 0) { loopCount2 = document.getElementsByClassName('loopCount2'); for (var k = 0; k < loopCount2.length; k++) { loopCount2[k].id = 'loopCount2_' + k;   } } var countXinterval2 = 0; if ($('.countXinterval2').length > 0) { countXinterval2 = document.getElementsByClassName('countXinterval2'); for (k = 0; k < countXinterval2.length; k++) { countXinterval2[k].id = 'countXinterval2_' + k;   } } var bText         = document.getElementsByClassName('bText'); var bDelayText    = document.getElementsByClassName('bDelayText'); var years         = document.getElementsByClassName('years'); var months        = document.getElementsByClassName('months'); var days          = document.getElementsByClassName('days'); var hours         = document.getElementsByClassName('hours'); var minutes       = document.getElementsByClassName('minutes'); var seconds       = document.getElementsByClassName('seconds'); var aText         = document.getElementsByClassName('aText'); var aDelayText    = document.getElementsByClassName('aDelayText'); var seedDate      = document.getElementsByClassName('seedDate'); var loopTime      = document.getElementsByClassName('loopTime'); var loopUnit      = document.getElementsByClassName('loopTimeUnit'); var delayTime     = document.getElementsByClassName('delayTime'); var delayUnit     = document.getElementsByClassName('delayTimeUnit'); var delayDisplay  = document.getElementsByClassName('delayCountDisplay'); var dst           = document.getElementsByClassName('dst'); var dateFormat    = document.getElementsByClassName('dateFormat'); var dateLabels    = document.getElementsByClassName('dateLabels'); var separators    = document.getElementsByClassName('separators'); for (k = 0; k < count.length; k++) { bText[k].id     = 'bText_'         + k; bDelayText[k].id   = 'bDelayText_'        + k;    years[k].id      = 'years_'         + k; months[k].id       = 'months_'            + k;    days[k].id       = 'days_'          + k; hours[k].id        = 'hours_'             + k;    minutes[k].id    = 'minutes_'       + k; seconds[k].id      = 'seconds_'           + k;    aText[k].id      = 'aText_'         + k; aDelayText[k].id   = 'aDelayText_'        + k;    seedDate[k].id   = 'seedDate_'      + k; loopTime[k].id     = 'loopTime_'          + k;    loopUnit[k].id   = 'loopTimeUnit_'  + k; delayTime[k].id    = 'delayTime_'         + k;    delayUnit[k].id  = 'delayTimeUnit_' + k; delayDisplay[k].id = 'delayCountDisplay_' + k;    dst[k].id        = 'dst_'           + k; dateFormat[k].id   = 'dateFormat_'        + k;    dateLabels[k].id = 'dateLabels_'    + k; separators[k].id   = 'separators_'        + k; } Countdown(seedDate, loopTime, loopUnit, delayTime, delayUnit, dateFormat, dateLabels, separators); setInterval(function { Countdown(seedDate, loopTime, loopUnit, delayTime, delayUnit, dateFormat, dateLabels, separators) }, 1000); /* FINNER/Countdown END */