MediaWiki:Gadget-timer.js: Difference between revisions
From Pengwings
No edit summary |
No edit summary |
||
| Line 9: | Line 9: | ||
function updateCountdown() { | function updateCountdown() { | ||
var $this = $(this), t = getDate($this.data("jst-time")), now = new Date(); | var $this = $(this), t = getDate($this.data("jst-time")), now = new Date(); | ||
if ($this.data("jst-since") == "1") { | |||
var tempvar = t; | |||
t = now; | |||
now = tempvar; | |||
} | |||
if (t && (t > now)) { | if (t && (t > now)) { | ||
var $d = $this.find(".jst-days"), $h = $this.find(".jst-hours"), $m = $this.find(".jst-minutes"), $s = $this.find(".jst-seconds"); | var $d = $this.find(".jst-days"), $h = $this.find(".jst-hours"), $m = $this.find(".jst-minutes"), $s = $this.find(".jst-seconds"); | ||
Latest revision as of 13:44, 16 February 2026
// [[Template:Time]], [[Template:Countdown]]
/* Scripts pcj and Surafbrov from https://warcraft.wiki.gg/wiki/MediaWiki:Common.js */
function timeInit() {
function getDate(s) {
s = s && s.match(/(\d{4})-(\d{2})-(\d{2}) (\d{1,2}):(\d{2})/);
return s && Date.UTC(parseInt(s[1]), parseInt(s[2])-1, parseInt(s[3]), parseInt(s[4]), parseInt(s[5]));
}
function updateCountdown() {
var $this = $(this), t = getDate($this.data("jst-time")), now = new Date();
if ($this.data("jst-since") == "1") {
var tempvar = t;
t = now;
now = tempvar;
}
if (t && (t > now)) {
var $d = $this.find(".jst-days"), $h = $this.find(".jst-hours"), $m = $this.find(".jst-minutes"), $s = $this.find(".jst-seconds");
var ofs = (t - now)/1000 | 0 + ($s.length ? 0 : 60), d = (ofs / 86400) | 0, h = (ofs / 3600) | 0, m = (ofs / 60) | 0, s = ofs % 60;
if ($d.length) h %= 24;
if ($h.length) m %= 60;
$d.toggleClass("jst-active", d).find(".jst-value").text(d);
$h.toggleClass("jst-active", d || h).find(".jst-value").text(h);
$m.toggleClass("jst-active", d || h || m).find(".jst-value").text(m);
$s.toggleClass("jst-active", true).find(".jst-value").text(s);
$this.addClass("jst-active");
} else {
$this.removeClass("jst-active");
$this.text($this.data('jst-text-over') || "");
}
}
function updateCountdowns() {
$(".jst-countdown.jst-active").each(updateCountdown);
if ($(".jst-countdown.jst-active").length) setTimeout(updateCountdowns, 1001);
}
$(".jst-countdown").addClass("jst-active");
$(".jst-countdown .jst-alternative").remove();
$(".jst-days > .jst-label").text("days");
$(".jst-hours > .jst-label").text("hours");
$(".jst-minutes > .jst-label").text("minutes");
$(".jst-seconds > .jst-label").text("seconds");
updateCountdowns();
$(".jst-abstime").each(function() {
var $this = $(this), t1 = getDate($this.data("jst-time")), t2 = getDate($this.data("jst-time2")), ta = getDate($this.data("jst-anchor"));
if (!t1) return;
var t1d = new Date(t1), nowDate = ta ? (new Date(ta)).toDateString() : (new Date()).toDateString();
$this.text((t1d.toDateString() == nowDate ? t1d.toLocaleTimeString() : (t1d.toLocaleDateString() + ", " + t1d.toLocaleTimeString())) + (t2 ? " – " + (new Date(t2)).toLocaleTimeString() : ""));
});
}
$(function() {
timeInit();
});