Advertisement
Guest User

jquery.countdown.js

a guest
Dec 17th, 2018
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. /*
  2. * jquery-countdown plugin - v0.2
  3. *
  4. * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
  5. * Copyright (c) 2011 Peter Farmer <pfarmer@gmail.com>
  6. *
  7. * Dual licensed under the MIT and GPL licenses.
  8. * http://docs.jquery.com/License
  9. */
  10.  
  11. (function( $ ) {
  12. var init = function( userOptions ) {
  13. var options = {
  14. stepTime: 60,
  15. format: "dd:hh:mm:ss",
  16. startTime: "01:12:32:55",
  17. digitImages: 6,
  18. digitWidth: 53,
  19. digitHeight: 77,
  20. autoStart: true,
  21. timerEnd: function() {
  22. },
  23. image: "digits.png"
  24. };
  25. var digits = [], interval;
  26.  
  27. // Draw digits in given container
  28. var createDigits = function(where) {
  29. var c = 0;
  30. var hCounter = 0;
  31. var mCounter = 0;
  32. var sCounter = 0;
  33.  
  34. // Check the incoming startTime
  35. // console.log("options.startTime = " + options.startTime);
  36.  
  37. if ((typeof options.startTime == 'object') && (options.startTime.constructor == Date)) {
  38. // console.log("Have been passed a date object? hopefully?");
  39. var now = new Date();
  40. if (options.startTime.getTime() < now.getTime()) {
  41. options.startTime.setFullYear(options.startTime.getFullYear() + 1);
  42. // console.log("options.startTime is now = " + options.startTime);
  43. }
  44. // console.log("options.startTime.getTime() = " + options.startTime.getTime());
  45. // console.log("now.getTime() = " + now.getTime());
  46. var datediff = Math.ceil((options.startTime.getTime() - now.getTime()) / 1000);
  47. // console.log("datediff = " + datediff);
  48. var days = Math.floor(datediff / 86400);
  49. // console.log("days = " + days);
  50. var hours = Math.floor((datediff % 86400) / 3600);
  51. var minutes = Math.floor(((datediff % 86400) % 3600) / 60);
  52. var seconds = ((datediff % 86400) % 3600) % 60;
  53. options.startTime = days + ":" + hours + ":" + minutes + ":" + seconds;
  54. }
  55.  
  56. _startTime = options.startTime.split("");
  57. // Count the number of ":" in the startTime.
  58. cCounter = 0;
  59. for (var i = 0; i < _startTime.length; i++) {
  60. if (isNaN(parseInt(_startTime[i]))) {
  61. // console.log("cCounter++ (" + _startTime[i] + ")");
  62. cCounter = cCounter + 1;
  63. }
  64. }
  65.  
  66. // Zero pad each section of the startTime if required.
  67. // console.log("options.startTime = " + options.startTime)
  68. var chunks = options.startTime.split(":");
  69. // console.log("chunks.length = " + chunks.length);
  70. var newstartTime = "";
  71. for (var i = 0; i < chunks.length; i++) {
  72. var max = 59;
  73. if (chunks.length == 3) {
  74. if (i == 0) {
  75. max = 23;
  76. }
  77. }
  78. if (chunks.length == 4) {
  79. if (i == 0) {
  80. max = 9999;
  81. }
  82. if (i == 1) {
  83. max = 23;
  84. }
  85. }
  86.  
  87. if (chunks[i] > max) {
  88. chunks[i] = max;
  89. }
  90. if (chunks[i].length < 2) {
  91. chunks[i] = "0" + chunks[i];
  92. }
  93. }
  94.  
  95. options.startTime = chunks.join(":");
  96. // console.log("options.startTime = " + options.startTime)
  97.  
  98. // Calculate what the format should be:
  99. switch (cCounter) {
  100. case 3:
  101. // console.log("options.startTime.split(':', 1).length is " + options.startTime.split(":")[0].length);
  102. if (options.startTime.split(":")[0].length == 3) {
  103. options.format = "ddd:hh:mm:ss";
  104. } else {
  105. options.format = "dd:hh:mm:ss";
  106. }
  107. break;
  108. case 2:
  109. options.format = "hh:mm:ss";
  110. break;
  111. case 1:
  112. options.format = "mm:ss";
  113. break;
  114. case 0:
  115. options.format = "ss";
  116. break;
  117. }
  118.  
  119. // console.log("cCounter == " + cCounter);
  120. // console.log("options.format == " + options.format);
  121.  
  122. // Iterate each startTime digit, if it is not a digit
  123. // we'll assume that it's a separator
  124. options.startTime = options.startTime.split("");
  125. options.format = options.format.split("");
  126. // console.log("options.startTime = " + options.startTime);
  127. // console.log("options.startTime.length = " + options.startTime.length);
  128. for (var i = 0; i < options.startTime.length; i++) {
  129. // console.log("options.startTime[" + i + "] = " + options.startTime[i]);
  130. if (parseInt(options.startTime[i]) >= 0) {
  131. // console.log("parseInt >= 0");
  132. var elem = jQuery('<div id="cnt_' + i + '" class="cntDigit" />').css({
  133. height: options.digitHeight * options.digitImages * 10,
  134. "float": 'left', background: 'url(\'' + options.image + '\')',
  135. width: options.digitWidth});
  136. // console.log("elem = " + elem);
  137. digits.push(elem);
  138. margin(c, -((parseInt(options.startTime[i]) * options.digitHeight *
  139. options.digitImages)));
  140. digits[c].__max = 9;
  141. // Add max digits, for example, first digit of minutes (mm) has
  142. // a max of 5. Conditional max is used when the left digit has reach
  143. // the max. For example second "hours" digit has a conditional max of 4
  144. // console.log("options.format[" + i + "] = " + options.format[i]);
  145. switch (options.format[i]) {
  146. case 'h':
  147. if (hCounter < 1) {
  148. // console.log("digits[c] = " + digits[c]);
  149. digits[c].__max = 2;
  150. // console.log("settings digits[" + c + "].__max = 2");
  151. hCounter = 1;
  152. } else {
  153. digits[c].__condmax = 3;
  154. // console.log("settings digits[" + c + "].__condmax = 3");
  155. }
  156. break;
  157. case 'd':
  158. digits[c].__max = 9;
  159. break;
  160. case 'm':
  161. if (mCounter < 1) {
  162. digits[c].__max = 5;
  163. mCounter = 1;
  164. } else {
  165. digits[c].__condmax = 9;
  166. }
  167. break;
  168. case 's':
  169. if (sCounter < 1) {
  170. digits[c].__max = 5;
  171. sCounter = 1;
  172. } else {
  173. digits[c].__condmax = 9;
  174. }
  175. break;
  176. }
  177. ++c;
  178. } else {
  179. elem = jQuery('<div class="cntSeparator"/>').css({"float": 'left'}).text(options.startTime[i]);
  180. }
  181. where.append('<div>');
  182. where.append(elem);
  183. where.append('</div>');
  184. }
  185. };
  186.  
  187. // Set or get element margin
  188. var margin = function(elem, val) {
  189. if (val !== undefined)
  190. return digits[elem].css({'marginTop': val + 'px'});
  191.  
  192. return parseInt(digits[elem].css('marginTop').replace('px', ''));
  193. };
  194.  
  195. // Makes the movement. This is done by "digitImages" steps.
  196. var moveStep = function(elem) {
  197. // console.log("digits[elem] = " + digits[elem]);
  198. digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
  199. return function _move() {
  200. mtop = margin(elem) + options.digitHeight;
  201. if (mtop == options.digitHeight) {
  202. margin(elem, digits[elem]._digitInitial);
  203. if (elem > 0) moveStep(elem - 1)();
  204. else {
  205. clearInterval(interval);
  206. for (var i = 0; i < digits.length; i++) margin(i, 0);
  207. options.timerEnd();
  208. return;
  209. }
  210. if ((elem > 0) && (digits[elem].__condmax !== undefined) &&
  211. (digits[elem - 1]._digitInitial == margin(elem - 1)))
  212. margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
  213. return;
  214. }
  215.  
  216. margin(elem, mtop);
  217. if (margin(elem) / options.digitHeight % options.digitImages != 0)
  218. setTimeout(_move, options.stepTime);
  219.  
  220. if (mtop == 0) digits[elem].__isma = true;
  221. }
  222. };
  223.  
  224. var start = function() {
  225. if (interval == undefined)
  226. interval = setInterval(moveStep(digits.length - 1), 1000);
  227. }
  228.  
  229. var pause = function() {
  230. if (interval) {
  231. window.clearInterval(interval);
  232. interval = undefined;
  233. }
  234. }
  235.  
  236. this.data("countdown", {
  237. "start": start,
  238. "pause": pause
  239. });
  240.  
  241. $.extend(options, userOptions);
  242. this.css({height: options.digitHeight, overflow: 'hidden'});
  243. createDigits(this);
  244. if (options.autoStart) {
  245. start();
  246. }
  247. };
  248.  
  249. $.fn.countdown = function( method ) {
  250. var methods = this.data("countdown");
  251. if ( methods && methods[method] ) {
  252. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  253. } else if ( typeof method === 'object' || ! method ) {
  254. return init.apply( this, arguments );
  255. } else {
  256. $.error( 'Method ' + method + ' does not exist on jQuery.countdown' );
  257. }
  258. };
  259.  
  260. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement