Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. var Countdown =
  3. {
  4.   timer: null,
  5.   init: function(id, until)
  6.   {
  7.     Countdown.node  = document.getElementById(id);
  8.     Countdown.update(until);
  9.     Countdown.timer = setInterval(function()
  10.     {
  11.       Countdown.update(until);
  12.     }, 1000);
  13.   },
  14.   stop: function()
  15.   {
  16.     clearInterval(Countdown.timer);
  17.     return true;
  18.   },
  19.   update: function(until)
  20.   {
  21.     var s = Countdown.process(new Date(), until);
  22.     Countdown.node.innerHTML = s || Countdown.stop() && 'Всё :)';
  23.   },
  24.   difference: function(before, after)
  25.   {
  26.     if (after < before) return false;
  27.     var
  28.     days   = after.getDate()  - before.getDate(),
  29.     months = after.getMonth() - before.getMonth(),
  30.     years  = after.getYear()  - before.getYear(),
  31.     hms    = (after / 1000 - before / 1000) % 86400,
  32.     seconds = Math.floor(hms % 60),
  33.     minutes = Math.floor(hms/60) % 60,
  34.     hours   = Math.floor(hms/3600) % 60,
  35.     date = new Date();
  36.     if (days < 0)
  37.     {
  38.       date.setFullYear(before.getYear(), before.getMonth(), 32);
  39.       days += 32 - date.getDate();
  40.       months--;
  41.     }
  42.     if (months < 0)
  43.     {
  44.       months += 12;
  45.       years--;
  46.     }
  47.     return {
  48.       years:   years,
  49.       months:  months,
  50.       days:    days,
  51.       hours:   hours,
  52.       minutes: minutes,
  53.       seconds: seconds
  54.     };
  55.   },
  56.   process: function(before, after)
  57.   {
  58.     var diff = null, a = [], i = '';
  59.     if(!(diff = Countdown.difference(before, after))) return false;
  60.     for(i in diff)
  61.     {
  62.       if(!diff[i]) continue;
  63.       a.push('<span class="num">'
  64.               + diff[i] +
  65.              '</span><span class="unit">'
  66.               + Countdown.lang[i][Countdown.lang.choose( diff[i] )] +
  67.              '</span>');
  68.     }
  69.     return a.join(' ');
  70.   },
  71.   lang:
  72.   {
  73.     years:   ['год', 'года', 'лет'],
  74.     months:  ['месяц', 'месяца', 'месяцев'],
  75.     days:    ['день', 'дня', 'дней'],
  76.     hours:   ['час', 'часа', 'часов'],
  77.     minutes: ['минута', 'минуты', 'минут'],
  78.     seconds: ['секунда', 'секунды', 'секунд'],
  79.     choose:  function(n)
  80.     {
  81.       var m = n % 100, i = 2;
  82.       if(m < 5 || 20 < m)
  83.       {
  84.         if((m %= 10) === 1)
  85.         {
  86.           i = 0;
  87.         }
  88.         else if(1 < m && m < 5)
  89.         {
  90.           i = 1;
  91.         }
  92.       }
  93.       return i;
  94.     }
  95.   }
  96. };
  97. window.onload = function()
  98. {
  99.   Countdown.init('countbox', new Date(2010, 10, 29, 1, 1, 00));
  100. };
  101. </script>
  102. </head>
  103. <body>
  104. <span id="countbox"></span>
  105. </body>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement