User:ToxiBoi/LiveCountdown.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:ToxiBoi/LiveCountdown. |
var MainLoop = function (index) {
var RefreshingAlready = false;
setInterval(function () {
// Setup again
endDate = new Date(index.getAttribute("data-end")).getTime();
now = new Date().getTime();
distance = endDate - now;
// Time calculations for days, hours, minutes and seconds (copied from W3Schools)
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result
index.innerHTML =
"There are <b>" +
days +
"</b> days, <b>" +
hours +
"</b> hours, <b>" +
minutes +
"</b> minutes, and <b>" +
seconds +
"</b> seconds until " +
index.getAttribute("data-event") +
".";
// If the count down is finished, refresh
if (distance < 0) {
index.innerHTML = "Countdown expired, refreshing...";
if (!RefreshingAlready) {
document.location.reload();
RefreshingAlready = true;
}
}
}, 1000);
};
var counts = document.getElementsByClassName("toxicountdown");
if (counts.length > 0) {
for (var i = 0; i < counts.length; i++) {
counts[i].innerHTML = "Loading countdown...";
var endDate = new Date(counts[i].getAttribute("data-end")).getTime();
var now = new Date().getTime();
var distance = endDate - now;
if (distance < 0) {
counts[i].innerHTML = "The countdown finished.";
} else {
MainLoop(counts[i]);
}
}
} else {
console.log("[LiveCountdown] No countdown widgets detected.");
}