MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
(trying to build a countdown clock) |
(forgot to close the document ready function) |
||
Line 49: | Line 49: | ||
initializeClock('clockdiv', deadline); | initializeClock('clockdiv', deadline); | ||
} | } | ||
} | } ); |
Revision as of 16:07, 19 July 2016
/* Any JavaScript here will be loaded for all users on every page load. */
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
/* only run this on the page that there is a clock div... */
// Or just a plain access for comparison
// (no need to check exists first, it falls back to null)
$( function () {
if ( mw.config.get( 'wgPageName' ) === 'Template:Countdown' ) {
var deadline = new Date(2016, 07, 05, 10, 0, 0, 0); /* base 0 months */
initializeClock('clockdiv', deadline);
}
} );