MediaWiki:Common.js: Difference between revisions

From Electromagnetic Field
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 2: Line 2:


function getTimeRemaining(endtime) {
function getTimeRemaining(endtime) {
   var t = Date.parse(endtime) - Date.parse(new Date());
   var t = -Date.parse(endtime) + Date.parse(new Date());
   var seconds = Math.floor((t / 1000) % 60);
   var seconds = Math.floor((t / 1000) % 60);
   var minutes = Math.floor((t / 1000 / 60) % 60);
   var minutes = Math.floor((t / 1000 / 60) % 60);

Revision as of 16:49, 9 August 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' || mw.config.get( 'wgPageName' ) === 'User:Stitch' || mw.config.get( 'wgPageName' ) === 'Electromagnetic_Field_2016' || mw.config.get( 'wgPageName' ) === 'User:MScroggs') {
    var deadline = new Date(2016, 07, 05, 10, 0, 0, 0); /* base 0 months */
    initializeClock('clockdiv', deadline);
}
} );