Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.countdown.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.Countdown=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. var defaultOptions = {
  3. date: "June 7, 2087 15:03:25",
  4. refresh: 1000,
  5. offset: 0,
  6. onEnd: function() {
  7. return;
  8. },
  9. render: function (date) {
  10. this.el.innerHTML = date.years + " years, " +
  11. date.days + " days, " +
  12. this.leadingZeros(date.hours) + " hours, " +
  13. this.leadingZeros(date.min) + " min and " +
  14. this.leadingZeros(date.sec) + " sec";
  15. }
  16. };
  17. /**
  18. * Countdown constructor
  19. * @param {HTMLElement} el DOM node of the countdown
  20. * @param {Object} options (optional) Options for the plugin
  21. */
  22. var Countdown = function(el, options) {
  23. /**
  24. * Reference to the DOM element
  25. * @type {HTMLElement}
  26. */
  27. this.el = el;
  28. /**
  29. * Options of the countdown plugin
  30. * @type {Object}
  31. */
  32. this.options = {};
  33. /**
  34. * Interval reference or false if counter is stopped
  35. * @type {Mixed}
  36. */
  37. this.interval = false;
  38. // merge default options and options into this.options
  39. this.mergeOptions = function(options) {
  40. for (var i in defaultOptions) {
  41. if (defaultOptions.hasOwnProperty(i)) {
  42. this.options[i] = typeof options[i] !== 'undefined' ? options[i] : defaultOptions[i];
  43. if (i === 'date' && typeof this.options.date !== 'object') {
  44. this.options.date = new Date(this.options.date);
  45. }
  46. // bind context for functions
  47. if (typeof this.options[i] === 'function') {
  48. this.options[i] = this.options[i].bind(this);
  49. }
  50. }
  51. }
  52. if (typeof this.options.date !== 'object') {
  53. this.options.date = new Date(this.options.date);
  54. }
  55. }.bind(this);
  56. this.mergeOptions(options);
  57. /**
  58. * Get the difference between now and the end date
  59. * @return {Object} Object with the diff information (years, days, hours, min, sec, millisec)
  60. */
  61. this.getDiffDate = function() {
  62. var diff = (this.options.date.getTime() - Date.now() + this.options.offset) / 1000;
  63. var dateData = {
  64. years: 0,
  65. days: 0,
  66. hours: 0,
  67. min: 0,
  68. sec: 0,
  69. millisec: 0
  70. };
  71. if (diff <= 0) {
  72. if (this.interval) {
  73. this.stop();
  74. this.options.onEnd();
  75. }
  76. return dateData;
  77. }
  78. if (diff >= (365.25 * 86400)) {
  79. dateData.years = Math.floor(diff / (365.25 * 86400));
  80. diff -= dateData.years * 365.25 * 86400;
  81. }
  82. if (diff >= 86400) {
  83. dateData.days = Math.floor(diff / 86400);
  84. diff -= dateData.days * 86400;
  85. }
  86. if (diff >= 3600) {
  87. dateData.hours = Math.floor(diff / 3600);
  88. diff -= dateData.hours * 3600;
  89. }
  90. if (diff >= 60) {
  91. dateData.min = Math.floor(diff / 60);
  92. diff -= dateData.min * 60;
  93. }
  94. dateData.sec = Math.round(diff);
  95. dateData.millisec = diff % 1 * 1000;
  96. return dateData;
  97. }.bind(this);
  98. /**
  99. * Add leading zeros to a number
  100. * @param {Number} num Input number
  101. * @param {Number} length Length of the desired output
  102. * @return {String} String of the desired length with leading zeros
  103. */
  104. this.leadingZeros = function(num, length) {
  105. length = length || 2;
  106. num = String(num);
  107. if (num.length > length) {
  108. return num;
  109. }
  110. return (Array(length + 1).join('0') + num).substr(-length);
  111. };
  112. /**
  113. * Update the end date of the countdown
  114. * @param {Mixed} newDate Date object or a String/Number that can be passed to the Date constructor
  115. * @return {Countdown} Countdown instance
  116. */
  117. this.update = function(newDate) {
  118. if (typeof newDate !== 'object') {
  119. newDate = new Date(newDate);
  120. }
  121. this.options.date = newDate;
  122. this.render();
  123. return this;
  124. }.bind(this);
  125. /**
  126. * Stop the countdown refresh / rerender
  127. * @return {Countdown} Countdown instance
  128. */
  129. this.stop = function() {
  130. if (this.interval) {
  131. clearInterval(this.interval);
  132. this.interval = false;
  133. }
  134. return this;
  135. }.bind(this);
  136. /**
  137. * Render the countdown
  138. * @return {Countdown} Countdown instance
  139. */
  140. this.render = function() {
  141. this.options.render(this.getDiffDate());
  142. return this;
  143. }.bind(this);
  144. /**
  145. * Start the countdown
  146. * @return {Countdown} Countdown instance
  147. */
  148. this.start = function() {
  149. // don't start if the countdown is already started
  150. if (this.interval) { return; }
  151. this.render();
  152. if (this.options.refresh) {
  153. this.interval = setInterval(this.render, this.options.refresh);
  154. }
  155. return this;
  156. }.bind(this);
  157. /**
  158. * Update the offset
  159. * @param {Number} offset New offset in ms
  160. * @return {Countdown} Countdown instance
  161. */
  162. this.updateOffset = function(offset) {
  163. this.options.offset = offset;
  164. return this;
  165. }.bind(this);
  166. /**
  167. * Restart the countdown and update options
  168. */
  169. this.restart = function (options) {
  170. this.mergeOptions(options);
  171. this.interval = false;
  172. this.start();
  173. return this;
  174. }.bind(this);
  175. // initial start of the countdown or initial render
  176. this.start();
  177. };
  178. module.exports = Countdown;
  179. },{}],2:[function(require,module,exports){
  180. var Countdown = require('./countdown.js');
  181. var NAME = 'countdown';
  182. var DATA_ATTR = 'date';
  183. jQuery.fn.countdown = function(options) {
  184. return $.each(this, function(i, el) {
  185. var $el = $(el);
  186. if (!$el.data(NAME)) {
  187. // allow setting the date via the data-date attribute
  188. if ($el.data(DATA_ATTR)) {
  189. options.date = $el.data(DATA_ATTR);
  190. }
  191. $el.data(NAME, new Countdown(el, options));
  192. }
  193. });
  194. };
  195. module.exports = Countdown;
  196. },{"./countdown.js":1}]},{},[2])(2)
  197. });