Jump to content

User:ToxiBoi/LiveCountdown.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by ToxiBoi (talk | contribs) at 23:25, 15 April 2020 (let's try timeout). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$( document ).ready(function() {
	setTimeout(function(){
		var counts = document.getElementsByClassName("toxicountdown");
		if (counts > 0) {
			for (var i = 0; i < counts.length; i++) {
			  var index = counts[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 {
			    var update = 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) {
			        clearInterval(update);
			        index.innerHTML = "Countdown expired, refreshing...";
			        document.location.reload();
			      }
			    }, 1000);
			  }
			}
		} else {
			console.log('[LiveCountdown] No countdown widgets detected.');
		}
	}, 1500);
});