JAVASCRIPT   122
jquery countdown js
Guest on 20th August 2022 01:01:14 AM


  1. ! function(a) {
  2.     "use strict";
  3.     "function" == typeof define && define.amd ? define(["jquery"], a) : a(jQuery)
  4. }(function(a) {
  5.     "use strict";
  6.  
  7.     function b(a) {
  8.         if (a instanceof Date) return a;
  9.         if (String(a).match(g)) return String(a).match(/^[0-9]*$/) && (a = Number(a)), String(a).match(/\-/) && (a = String(a).replace(/\-/g, "index.html")), new Date(a);
  10.         throw new Error("Couldn't cast `" + a + "` to a date object.")
  11.     }
  12.  
  13.     function c(a) {
  14.         var b = a.toString().replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
  15.         return new RegExp(b)
  16.     }
  17.  
  18.     function d(a) {
  19.         return function(b) {
  20.             var d = b.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);
  21.             if (d)
  22.                 for (var f = 0, g = d.length; f < g; ++f) {
  23.                     var h = d[f].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/),
  24.                         j = c(h[0]),
  25.                         k = h[1] || "",
  26.                         l = h[3] || "",
  27.                         m = null;
  28.                     h = h[2], i.hasOwnProperty(h) && (m = i[h], m = Number(a[m])), null !== m && ("!" === k && (m = e(l, m)), "" === k && m < 10 && (m = "0" + m.toString()), b = b.replace(j, m.toString()))
  29.                 }
  30.             return b = b.replace(/%%/, "%")
  31.         }
  32.     }
  33.  
  34.     function e(a, b) {
  35.         var c = "s",
  36.             d = "";
  37.         return a && (a = a.replace(/(:|;|\s)/gi, "").split(/\,/), 1 === a.length ? c = a[0] : (d = a[0], c = a[1])), Math.abs(b) > 1 ? c : d
  38.     }
  39.     var f = [],
  40.         g = [],
  41.         h = {
  42.             precision: 100,
  43.             elapse: !1,
  44.             defer: !1
  45.         };
  46.     g.push(/^[0-9]*$/.source), g.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source), g.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source), g = new RegExp(g.join("|"));
  47.     var i = {
  48.             Y: "years",
  49.             m: "months",
  50.             n: "daysToMonth",
  51.             d: "daysToWeek",
  52.             w: "weeks",
  53.             W: "weeksToMonth",
  54.             H: "hours",
  55.             M: "minutes",
  56.             S: "seconds",
  57.             D: "totalDays",
  58.             I: "totalHours",
  59.             N: "totalMinutes",
  60.             T: "totalSeconds"
  61.         },
  62.         j = function(b, c, d) {
  63.             this.el = b, this.$el = a(b), this.interval = null, this.offset = {}, this.options = a.extend({}, h), this.instanceNumber = f.length, f.push(this), this.$el.data("countdown-instance", this.instanceNumber), d && ("function" == typeof d ? (this.$el.on("update.countdown", d), this.$el.on("stoped.countdown", d), this.$el.on("finish.countdown", d)) : this.options = a.extend({}, h, d)), this.setFinalDate(c), this.options.defer === !1 && this.start()
  64.         };
  65.     a.extend(j.prototype, {
  66.         start: function() {
  67.             null !== this.interval && clearInterval(this.interval);
  68.             var a = this;
  69.             this.update(), this.interval = setInterval(function() {
  70.                 a.update.call(a)
  71.             }, this.options.precision)
  72.         },
  73.         stop: function() {
  74.             clearInterval(this.interval), this.interval = null, this.dispatchEvent("stoped")
  75.         },
  76.         toggle: function() {
  77.             this.interval ? this.stop() : this.start()
  78.         },
  79.         pause: function() {
  80.             this.stop()
  81.         },
  82.         resume: function() {
  83.             this.start()
  84.         },
  85.         remove: function() {
  86.             this.stop.call(this), f[this.instanceNumber] = null, delete this.$el.data().countdownInstance
  87.         },
  88.         setFinalDate: function(a) {
  89.             this.finalDate = b(a)
  90.         },
  91.         update: function() {
  92.             if (0 === this.$el.closest("html").length) return void this.remove();
  93.             var b, c = void 0 !== a._data(this.el, "events"),
  94.                 d = new Date;
  95.             b = this.finalDate.getTime() - d.getTime(), b = Math.ceil(b / 1e3), b = !this.options.elapse && b < 0 ? 0 : Math.abs(b), this.totalSecsLeft !== b && c && (this.totalSecsLeft = b, this.elapsed = d >= this.finalDate, this.offset = {
  96.                 seconds: this.totalSecsLeft % 60,
  97.                 minutes: Math.floor(this.totalSecsLeft / 60) % 60,
  98.                 hours: Math.floor(this.totalSecsLeft / 60 / 60) % 24,
  99.                 days: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  100.                 daysToWeek: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  101.                 daysToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 % 30.4368),
  102.                 weeks: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7),
  103.                 weeksToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7) % 4,
  104.                 months: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 30.4368),
  105.                 years: Math.abs(this.finalDate.getFullYear() - d.getFullYear()),
  106.                 totalDays: Math.floor(this.totalSecsLeft / 60 / 60 / 24),
  107.                 totalHours: Math.floor(this.totalSecsLeft / 60 / 60),
  108.                 totalMinutes: Math.floor(this.totalSecsLeft / 60),
  109.                 totalSeconds: this.totalSecsLeft
  110.             }, this.options.elapse || 0 !== this.totalSecsLeft ? this.dispatchEvent("update") : (this.stop(), this.dispatchEvent("finish")))
  111.         },
  112.         dispatchEvent: function(b) {
  113.             var c = a.Event(b + ".countdown");
  114.             c.finalDate = this.finalDate, c.elapsed = this.elapsed, c.offset = a.extend({}, this.offset), c.strftime = d(this.offset), this.$el.trigger(c)
  115.         }
  116.     }), a.fn.countdown = function() {
  117.         var b = Array.prototype.slice.call(arguments, 0);
  118.         return this.each(function() {
  119.             var c = a(this).data("countdown-instance");
  120.             if (void 0 !== c) {
  121.                 var d = f[c],
  122.                     e = b[0];
  123.                 j.prototype.hasOwnProperty(e) ? d[e].apply(d, b.slice(1)) : null === String(e).match(/^[$A-Z_][0-9A-Z_$]*$/i) ? (d.setFinalDate.call(d, e), d.start()) : a.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi, e))
  124.             } else new j(this, b[0], b[1])
  125.         })
  126.     }
  127. });

Raw Paste

Login or Register to edit or fork this paste. It's free.