jquery.countdown.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function ($) {
  2. $.fn.countdown = function () {
  3. var data = "";
  4. var _DOM = null;
  5. var TIMER;
  6. createdom = function (dom) {
  7. _DOM = dom;
  8. data = $(dom).attr("data-time");
  9. if (data) {
  10. data = data.replace(/-/g, "/");
  11. data = Math.round((new Date(data)).getTime() / 1000);
  12. reflash();
  13. }
  14. };
  15. reflash = function () {
  16. var range = data - Math.round((new Date()).getTime() / 1000),
  17. secday = 86400, sechour = 3600,
  18. days = parseInt(range / secday),
  19. hours = parseInt((range % secday) / sechour),
  20. min = parseInt(((range % secday) % sechour) / 60),
  21. sec = ((range % secday) % sechour) % 60;
  22. $(_DOM).find(".days").html(nol(days));
  23. $(_DOM).find(".hours").html(nol(hours));
  24. $(_DOM).find(".minutes").html(nol(min));
  25. $(_DOM).find(".seconds").html(nol(sec));
  26. };
  27. TIMER = setInterval(reflash, 1000);
  28. nol = function (h) {
  29. if (h < 0) {
  30. h = '0' + '0';
  31. } else if (h < 10) {
  32. h = '0' + h;
  33. }
  34. return h;
  35. }
  36. return this.each(function () {
  37. var $box = $(this);
  38. createdom($box);
  39. });
  40. }
  41. })(jQuery);