countdowntime.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function ($) {
  2. "use strict";
  3. $.fn.extend({
  4. countdown100: function(options) {
  5. var defaults = {
  6. timeZone: "",
  7. endtimeYear: 0,
  8. endtimeMonth: 0,
  9. endtimeDate: 0,
  10. endtimeHours: 0,
  11. endtimeMinutes: 0,
  12. endtimeSeconds: 0,
  13. }
  14. var options = $.extend(defaults, options);
  15. return this.each(function() {
  16. var obj = $(this);
  17. var timeNow = new Date();
  18. var tZ = options.timeZone;
  19. var endYear = options.endtimeYear;
  20. var endMonth = options.endtimeMonth;
  21. var endDate = options.endtimeDate;
  22. var endHours = options.endtimeHours;
  23. var endMinutes = options.endtimeMinutes;
  24. var endSeconds = options.endtimeSeconds;
  25. if(tZ == "") {
  26. var deadline = new Date(endYear, endMonth - 1, endDate, endHours, endMinutes, endSeconds);
  27. }
  28. else {
  29. var deadline = moment.tz([endYear, endMonth - 1, endDate, endHours, endMinutes, endSeconds], tZ).format();
  30. }
  31. if(Date.parse(deadline) < Date.parse(timeNow)) {
  32. var deadline = new Date(Date.parse(new Date()) + endDate * 24 * 60 * 60 * 1000 + endHours * 60 * 60 * 1000);
  33. }
  34. initializeClock(deadline);
  35. function getTimeRemaining(endtime) {
  36. var t = Date.parse(endtime) - Date.parse(new Date());
  37. var seconds = Math.floor((t / 1000) % 60);
  38. var minutes = Math.floor((t / 1000 / 60) % 60);
  39. var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  40. var days = Math.floor(t / (1000 * 60 * 60 * 24));
  41. return {
  42. 'total': t,
  43. 'days': days,
  44. 'hours': hours,
  45. 'minutes': minutes,
  46. 'seconds': seconds
  47. };
  48. }
  49. function initializeClock(endtime) {
  50. var daysSpan = $(obj).find('.days');
  51. var hoursSpan = $(obj).find('.hours');
  52. var minutesSpan = $(obj).find('.minutes');
  53. var secondsSpan = $(obj).find('.seconds');
  54. function updateClock() {
  55. var t = getTimeRemaining(endtime);
  56. daysSpan.html(t.days);
  57. hoursSpan.html(('0' + t.hours).slice(-2));
  58. minutesSpan.html(('0' + t.minutes).slice(-2));
  59. secondsSpan.html(('0' + t.seconds).slice(-2))
  60. if (t.total <= 0) {
  61. clearInterval(timeinterval);
  62. }
  63. }
  64. updateClock();
  65. var timeinterval = setInterval(updateClock, 1000);
  66. }
  67. });
  68. }
  69. });
  70. })(jQuery);