Advertisement
aaaaaa123456789

Countdown JS code for Acid Forums

Feb 11th, 2016
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.   var server_time = 1234567890123; // set to server time via PHP or something
  3.   var offset = (new Date()).valueOf() - server_time;
  4.  
  5.   var zeropad = function (number, len) {
  6.     return ('0'.repeat(len) + number.toString()).substr(-len);
  7.   }
  8.  
  9.   var countdown_text = function (limit, mode) {
  10.     // mode: -1: count only down, 0: down and up (with sign), 1: only up
  11.     limit += offset;
  12.     var current = (new Date()).valueOf();
  13.     var difference = Math.floor((current - limit) / 1000);
  14.     if (((difference < 0) && (mode > 0)) || ((difference > 0) && (mode < 0))) return ':00';
  15.     var sign = '';
  16.     if (difference < 0) {
  17.       if (mode == 0) sign = '-';
  18.       difference = -difference;
  19.     }
  20.     var result = ':' + zeropad(difference % 60, 2);
  21.     difference = Math.floor(difference / 60);
  22.     if (difference < 60) {
  23.       if (difference > 0) result = difference.toString() + result;
  24.     } else {
  25.       result = zeropad(difference % 60, 2) + result;
  26.       difference = Math.floor(difference / 60);
  27.       if (difference < 24)
  28.         result = difference.toString() + ':' + result;
  29.       else {
  30.         result = ' ' + zeropad(difference % 24, 2) + ':' + result;
  31.         difference = Math.floor(difference / 24);
  32.         if (difference == 1)
  33.           result = '1 day,' + result;
  34.         else
  35.           result = difference.toString() + ' days' + result;
  36.       }
  37.     }
  38.     return sign + result;
  39.   }
  40.  
  41.   var calculate_limit = function (limit_string) {
  42.     if (limit_string.length == 0) return undefined;
  43.     var mode = -1;
  44.     if (limit_string.charAt(0) == '+')
  45.       mode = 1;
  46.     else if (limit_string.charAt(0) == '-')
  47.       mode = 0;
  48.     var components = limit_string.split(/[^0-9]+/).filter(function (str) {return str.length;}).slice(0, 7).map(parseFloat);
  49.     if (components.length == 0) return undefined;
  50.     var date = new Date(0);
  51.     for (var index = 0; index < components.length; index ++) {
  52.       if (index == 1) components[index] --;
  53.       date[(['setUTCFullYear', 'setUTCMonth', 'setUTCDate', 'setUTCHours', 'setUTCMinutes', 'setUTCSeconds', 'setUTCMilliseconds'])[index]](components[index]);
  54.     }
  55.     var limit = date.valueOf();
  56.     if (isNaN(limit)) return undefined;
  57.     return {limit: limit, mode: mode};
  58.   }
  59.  
  60.   var update_count = function (element) {
  61.     if (!element.hasAttribute('data-limit')) return;
  62.     var limit = calculate_limit(element.attributes['data-limit'].value);
  63.     if (limit == undefined) return;
  64.     element.innerHTML = countdown_text(limit.limit, limit.mode);
  65.   }
  66.    
  67.   setInterval(function () {Array.prototype.forEach.call(document.querySelectorAll('span.xx-countdown'), update_count);}, 165);
  68. })();
Advertisement
RAW Paste Data Copied
Advertisement