jquery.countdown.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. jQuery.fn.extend({
  2. countdown: function(options) {
  3. options = options || {};
  4. options.end = Number(options.end) || "";
  5. options.interval = options.interval || 1000;
  6. options.ontimeout = options.ontimeout || function() { };
  7. options.global = options.global || 0;
  8. options.format = options.format || 'm'; //可以是Time,或者Minute
  9. options.inputType = options.inputType || 'm';
  10. var FORMATID = "format";
  11. this.data(FORMATID, options.format);
  12. this.data('ss', options.ontimeout);
  13. jQuery.countdowInterval = options.interval;
  14. jQuery.onCountdown = function(container, count) {
  15. container.html(jQuery.timeTranform(count <= 0 ? 0 : count, container.data(FORMATID)));
  16. if (count <= 0)
  17. container.data('ss')(); //options.ontimeout();
  18. }
  19. if (isNaN(options.end))
  20. return;
  21. var count = jQuery.fromTimes(options.end, options.inputType);
  22. return this.each(function() {
  23. var me = $(this);
  24. me.data(jQuery.STOPPOINTID, 0);
  25. jQuery.stopwatch(me, count);
  26. });
  27. },
  28. calcTimer: function(options) {
  29. options = options || {};
  30. options.limit = options.limit || "";
  31. options.interval = options.interval || 1000;
  32. options.ontimeout = options.ontimeout || function() { };
  33. options.onCountdownError = options.onCountdownError || function() { };
  34. options.format = options.format || 's'; //可以是Time,或者Minute
  35. var FORMATID = "format";
  36. this.data(FORMATID, options.format);
  37. this.data('ss', options.ontimeout);
  38. jQuery.countdowInterval = options.interval;
  39. jQuery.onCountdownErr = function(container) {
  40. options.onCountdownError(container);
  41. }
  42. jQuery.onCountdown = function(container, count) {
  43. container.html(jQuery.timeTranform(count <= 0 ? 0 : count, container.data(FORMATID)));
  44. if (count <= 0)
  45. container.data('ss')(); //options.ontimeout();
  46. }
  47. if (isNaN(options.limit))
  48. return;
  49. return this.each(function() {
  50. var me = $(this);
  51. jQuery.calcSecond(me, options.limit);
  52. });
  53. },
  54. addminute: function(minute) {
  55. var sec = jQuery.fromMinutes(minute);
  56. return this.each(function() {
  57. var me = $(this);
  58. jQuery.stopwatch(me, sec);
  59. });
  60. },
  61. gettime: function() {
  62. return this.data(jQuery.STOPPOINTID);
  63. },
  64. clear: function() {
  65. if (this.data(jQuery.INTERVALID))
  66. window.clearInterval(this.data(jQuery.INTERVALID));
  67. }
  68. });
  69. jQuery.extend({
  70. INTERVALID: "intervalID",
  71. STOPPOINTID: "stopPoint",
  72. LASTPOINTID: "lastPoint",
  73. countdowInterval: 1000,
  74. onCountdown: function(container, count) { },
  75. onCountdownErr: function(container) { },
  76. calcSecond: function(container, sec) {
  77. var me = this;
  78. var intervalID = container.data(me.INTERVALID);
  79. if (intervalID)
  80. window.clearInterval(intervalID);
  81. var now = new Date();
  82. // 根据传入参数计算结束时间
  83. var stopPoint = now.addSeconds(sec);
  84. container.data(me.STOPPOINTID, stopPoint);
  85. // 如果当前时间已经大于结束时间,则返回0标识结束
  86. if (stopPoint <= now) {
  87. me.onCountdown(container, 0);
  88. }
  89. else {
  90. // 间隔指定时间倒计时
  91. intervalID = window.setInterval(function() {
  92. var endPoint = container.data(me.STOPPOINTID);
  93. var curDate = new Date();
  94. if (curDate >= endPoint) {
  95. window.clearInterval(container.data(me.INTERVALID));
  96. me.onCountdown(container, 0);
  97. }
  98. else {
  99. var point = curDate.dateDiff('s', endPoint);
  100. // 检查上次时间是否小于本次时间,若是,表示用户修改了本地计算机时间
  101. var lastPoint = container.data(me.LASTPOINTID);
  102. if (lastPoint && lastPoint < point)
  103. me.onCountdownErr(container);
  104. container.data(me.LASTPOINTID, point);
  105. me.onCountdown(container, point);
  106. }
  107. }, me.countdowInterval);
  108. container.data(me.INTERVALID, intervalID);
  109. }
  110. },
  111. stopwatch: function(container, limit) {
  112. var me = this;
  113. // 如果已经开始计时,则清除后重新计算
  114. var intervalID = container.data(me.INTERVALID);
  115. if (intervalID != null) {
  116. window.clearInterval(intervalID);
  117. }
  118. // 计算总计数,并累加至内存中
  119. var stopPoint = container.data(me.STOPPOINTID);
  120. if (stopPoint == null)
  121. stopPoint = 0;
  122. stopPoint = stopPoint + limit;
  123. container.data(me.STOPPOINTID, stopPoint);
  124. if (stopPoint <= 0) {
  125. //stopPoint = 0;
  126. me.onCountdown(container, stopPoint);
  127. return;
  128. }
  129. // 开始倒计时,当到达0时停止
  130. intervalID = window.setInterval(function() {
  131. stopPoint = stopPoint - 1;
  132. if (Number(stopPoint) <= 0) {
  133. window.clearInterval(container.data(me.INTERVALID));
  134. }
  135. container.data(me.STOPPOINTID, stopPoint);
  136. me.onCountdown(container, stopPoint);
  137. }, me.countdowInterval);
  138. container.data(me.INTERVALID, intervalID);
  139. },
  140. timeTranform: function(count, format) {
  141. if (format == null)
  142. return count;
  143. var unit = format.toLowerCase();
  144. if (unit == "h" || unit == "hour")
  145. return Math.floor(count / 3600);
  146. if (unit == "m" || unit == "minute")
  147. return Math.floor(count / 60);
  148. if (unit == "s" || unit == "second")
  149. return count;
  150. return this.timeFormatTranformer(count, format);
  151. },
  152. timeFormatTranformer: function(count, format) {
  153. var hours = Math.floor(count / 3600);
  154. var minute = Math.floor(count % 3600 / 60);
  155. var second = Math.floor(count % 3600 % 60);
  156. return format.replace(/#h/i, hours).replace(/#m/i, minute).replace(/#s/i, second);
  157. },
  158. fromTimes: function(count, format) {
  159. if (/^s$/i.test(format))
  160. return count;
  161. if (/^m$/i.test(format))
  162. return this.fromMinutes(count);
  163. if (/^h$/i.test(format))
  164. return this.fromHours(count);
  165. },
  166. fromMinutes: function(minute) {
  167. return minute * 60;
  168. },
  169. fromHours: function(hours) {
  170. return hours * 3600;
  171. }
  172. });
  173. Date.prototype.add = function (milliseconds) {
  174. var m = this.getTime() + milliseconds;
  175. return new Date(m);
  176. };
  177. Date.prototype.addSeconds = function (second) {
  178. return this.add(second * 1000);
  179. };
  180. Date.prototype.addMinutes = function (minute) {
  181. return this.addSeconds(minute * 60);
  182. };
  183. Date.prototype.addHours = function (hour) {
  184. return this.addMinutes(60 * hour);
  185. };
  186. Date.prototype.dateDiff = function (interval, objDate2) {
  187. var d = this, i = {}, t = d.getTime(), t2 = objDate2.getTime();
  188. i['y'] = objDate2.getFullYear() - d.getFullYear();
  189. i['q'] = i['y'] * 4 + Math.floor(objDate2.getMonth() / 4) - Math.floor(d.getMonth() / 4);
  190. i['m'] = i['y'] * 12 + objDate2.getMonth() - d.getMonth();
  191. i['ms'] = objDate2.getTime() - d.getTime();
  192. i['w'] = Math.floor((t2 + 345600000) / (604800000)) - Math.floor((t + 345600000) / (604800000));
  193. i['d'] = Math.floor(t2 / 86400000) - Math.floor(t / 86400000);
  194. i['h'] = Math.floor(t2 / 3600000) - Math.floor(t / 3600000);
  195. i['n'] = Math.floor(t2 / 60000) - Math.floor(t / 60000);
  196. i['s'] = Math.floor(t2 / 1000) - Math.floor(t / 1000);
  197. return i[interval];
  198. };
  199. Date.prototype.Format = function (formatStr) {
  200. var str = formatStr;
  201. var Week = ['日', '一', '二', '三', '四', '五', '六'];
  202. str = str.replace(/yyyy|YYYY/, this.getFullYear());
  203. str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
  204. var month = this.getMonth() + 1;
  205. str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
  206. str = str.replace(/M/g, month);
  207. str = str.replace(/w|W/g, Week[this.getDay()]);
  208. str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
  209. str = str.replace(/d|D/g, this.getDate());
  210. str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
  211. str = str.replace(/h|H/g, this.getHours());
  212. str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
  213. str = str.replace(/m/g, this.getMinutes());
  214. str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
  215. str = str.replace(/s|S/g, this.getSeconds());
  216. return str;
  217. }