MediaWiki:Common.js/countdown.js

/* Any JavaScript here will be loaded for all users on every page load. Adds reusable timer. */ var Countdown = (function {   // Constructor    function Countdown(target) {        // Localise time fetched from data-countdown-time.        var oldDate = new Date(parseInt(target.data("countdown-time"), 10));        var newDate = new Date(oldDate.getTime + oldDate.getTimezoneOffset * 60000);        newDate.setHours(oldDate.getHours - (oldDate.getTimezoneOffset / 60));        // Store time        this._date = newDate;        // Store target (is already jQuery wrapped)        this._target = target;        // Set up events        setInterval(this.update, 1000);    }    // Update displayed time    Countdown.prototype.update = function {        // Calculate time remaining.        var timeleft = Math.floor(this._date.getTime / 1000) - Math.floor(new Date.getTime / 1000);        // If timeleft is 0, set value to data-countdown-endmsg, and end interval loop.        if (timeleft === 0) { this._target.html(this._target.data("time-end")); clearInterval; }       // Extract units. var releaseDays, releaseMinutes, releaseSeconds; daysLeft = Math.floor(timeleft / 86400); timeleft %= 86400; hoursLeft = Math.floor(timeleft / 3600); timeleft %= 3600; minutesLeft = Math.floor(timeleft / 60); secondsLeft = Math.floor(timeleft % 60); // Build readable string. var content = daysLeft + "d " + hoursLeft + "h " + minutesLeft + "m " + secondsLeft + "s"; this._target.html(content); }.bind(this); });

// Match all DOM elements with a specific class, and apply Countdown. $(".countdown").each(function(target) {   new Countdown(target); });