jquery.downCount.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * downCount: Simple Countdown clock with offset
  3. * Author: Sonny T. <hi@sonnyt.com>, sonnyt.com
  4. */
  5. (function ($) {
  6. $.fn.downCount = function (options, callback) {
  7. var settings = $.extend({
  8. date: null,
  9. offset: null
  10. }, options);
  11. // Throw error if date is not set
  12. if (!settings.date) {
  13. $.error('Date is not defined.');
  14. }
  15. // Throw error if date is set incorectly
  16. if (!Date.parse(settings.date)) {
  17. $.error('Incorrect date format, it should look like this, 12/24/2012 12:00:00.');
  18. }
  19. // Save container
  20. var container = this;
  21. /**
  22. * Change client's local date to match offset timezone
  23. * @return {Object} Fixed Date object.
  24. */
  25. var currentDate = function () {
  26. // get client's current date
  27. var date = new Date();
  28. // turn date to utc
  29. var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
  30. // set new Date object
  31. var new_date = new Date(utc + (3600000*settings.offset))
  32. return new_date;
  33. };
  34. /**
  35. * Main downCount function that calculates everything
  36. */
  37. function countdown () {
  38. var target_date = new Date(settings.date), // set target date
  39. current_date = currentDate(); // get fixed current date
  40. // difference of dates
  41. var difference = target_date - current_date;
  42. // if difference is negative than it's pass the target date
  43. if (difference < 0) {
  44. // stop timer
  45. clearInterval(interval);
  46. if (callback && typeof callback === 'function') callback();
  47. return;
  48. }
  49. // basic math variables
  50. var _second = 1000,
  51. _minute = _second * 60,
  52. _hour = _minute * 60,
  53. _day = _hour * 24;
  54. // calculate dates
  55. var days = Math.floor(difference / _day),
  56. hours = Math.floor((difference % _day) / _hour),
  57. minutes = Math.floor((difference % _hour) / _minute),
  58. seconds = Math.floor((difference % _minute) / _second);
  59. // fix dates so that it will show two digets
  60. days = (String(days).length >= 2) ? days : days;
  61. hours = (String(hours).length >= 2) ? hours : '0' + hours;
  62. minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
  63. seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;
  64. // based on the date change the refrence wording
  65. var ref_days = (days === 1) ? 'day' : 'days',
  66. ref_hours = (hours === 1) ? 'hour' : 'hours',
  67. ref_minutes = (minutes === 1) ? 'minute' : 'minutes',
  68. ref_seconds = (seconds === 1) ? 'second' : 'seconds';
  69. // set to DOM
  70. container.find('.days').text(days);
  71. container.find('.hours').text(hours);
  72. container.find('.minutes').text(minutes);
  73. container.find('.seconds').text(seconds);
  74. container.find('.days_ref').text(ref_days);
  75. container.find('.hours_ref').text(ref_hours);
  76. container.find('.minutes_ref').text(ref_minutes);
  77. container.find('.seconds_ref').text(ref_seconds);
  78. };
  79. // start
  80. var interval = setInterval(countdown, 1000);
  81. };
  82. })(jQuery);