Guest User

countdown jquery plugin

a guest
Nov 3rd, 2012
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Generated by CoffeeScript 1.3.3
  2.  
  3. /*
  4. countdown is a simple jquery plugin for countdowns
  5.  
  6. Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  7. and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  8.  
  9. @source: http://github.com/rendro/countdown/
  10. @autor: Robert Fleischmann
  11. @version: 1.0.0
  12. */
  13.  
  14.  
  15. (function() {
  16.  
  17.   (function($) {
  18.     $.countdown = function(el, options) {
  19.       var getDateData,
  20.         _this = this;
  21.       this.el = el;
  22.       this.$el = $(el);
  23.       this.$el.data("countdown", this);
  24.       this.init = function() {
  25.         _this.options = $.extend({}, $.countdown.defaultOptions, options);
  26.         if (_this.options.refresh) {
  27.           _this.interval = setInterval(function() {
  28.             return _this.render();
  29.           }, _this.options.refresh);
  30.         }
  31.         _this.render();
  32.         return _this;
  33.       };
  34.       getDateData = function(endDate) {
  35.         var dateData, diff;
  36.         endDate = Date.parse($.isPlainObject(_this.options.date) ? _this.options.date : new Date(_this.options.date));
  37.         diff = (endDate - Date.parse(new Date)) / 1000;
  38.         if (diff < 0) {
  39.           diff = 0;
  40.           if (_this.interval) {
  41.             _this.stop();
  42.           }
  43.         }
  44.         dateData = {
  45.           years: 0,
  46.           days: 0,
  47.           hours: 0,
  48.           min: 0,
  49.           sec: 0,
  50.           millisec: 0
  51.         };
  52.         if (diff >= (365.25 * 86400)) {
  53.           dateData.years = Math.floor(diff / (365.25 * 86400));
  54.           diff -= dateData.years * 365.25 * 86400;
  55.         }
  56.         if (diff >= 86400) {
  57.           dateData.days = Math.floor(diff / 86400);
  58.           diff -= dateData.days * 86400;
  59.         }
  60.         if (diff >= 3600) {
  61.           dateData.hours = Math.floor(diff / 3600);
  62.           diff -= dateData.hours * 3600;
  63.         }
  64.         if (diff >= 60) {
  65.           dateData.min = Math.floor(diff / 60);
  66.           diff -= dateData.min * 60;
  67.         }
  68.         dateData.sec = diff;
  69.         return dateData;
  70.       };
  71.       this.leadingZeros = function(num, length) {
  72.         if (length == null) {
  73.           length = 2;
  74.         }
  75.         num = String(num);
  76.         while (num.length < length) {
  77.           num = "0" + num;
  78.         }
  79.         return num;
  80.       };
  81.       this.update = function(newDate) {
  82.         _this.options.date = newDate;
  83.         return _this;
  84.       };
  85.       this.render = function() {
  86.         _this.options.render.apply(_this, [getDateData(_this.options.date)]);
  87.         return _this;
  88.       };
  89.       this.stop = function() {
  90.         if (_this.interval) {
  91.           clearInterval(_this.interval);
  92.         }
  93.         _this.interval = null;
  94.         return _this;
  95.       };
  96.       this.start = function(refresh) {
  97.         if (refresh == null) {
  98.           refresh = _this.options.refresh || $.countdown.defaultOptions.refresh;
  99.         }
  100.         if (_this.interval) {
  101.           clearInterval(_this.interval);
  102.         }
  103.         _this.render();
  104.         _this.options.refresh = refresh;
  105.         _this.interval = setInterval(function() {
  106.           return _this.render();
  107.         }, _this.options.refresh);
  108.         return _this;
  109.       };
  110.       return this.init();
  111.     };
  112.     $.countdown.defaultOptions = {
  113.       date: "June 7, 2087 15:03:25",
  114.       refresh: 1000,
  115.       render: function(date) {
  116.         _hey_html = "";
  117.         if(date.years > 0){
  118.           _hey_html += '<span class="countdown-years">' + date.years + 'year </span>';
  119.         }
  120.         return $(this.el).html(_hey_html+'<span class="countdown-days">  ' + date.days + '<small>d</small> </span><span class="countdown-hours"> ' + (this.leadingZeros(date.hours)) + '<small>h</small> </span><span class="countdown-min">' + (this.leadingZeros(date.min)) + '<small>m</small> </span><span class="countdown-sec">' + (this.leadingZeros(date.sec)) + '<small>s</small></span>');
  121.       }
  122.     };
  123.     $.fn.countdown = function(options) {
  124.       return $.each(this, function(i, el) {
  125.         var $el;
  126.         $el = $(el);
  127.         if (!$el.data('countdown')) {
  128.           return $el.data('countdown', new $.countdown(el, options));
  129.         }
  130.       });
  131.     };
  132.     return void 0;
  133.   })(jQuery);
  134.  
  135. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment