countdown.js 976 B

1234567891011121314151617181920212223242526272829303132333435
  1. function countdown(d) {
  2. var target = document.getElementById('countdown');
  3. var diff, dd, dh, dm, ds;
  4. function full_update() {
  5. diff = (new Date(Date.parse(d))-new Date())/1000;
  6. update_target();
  7. }
  8. function update() {
  9. diff -= 1;
  10. update_target();
  11. }
  12. function update_target() {
  13. var diff1, diff2;
  14. if (diff <= 0) {
  15. target.innerHTML = 'countdown finished';
  16. target.parentNode.setAttribute('href', "");
  17. return;
  18. }
  19. dd = parseInt(diff / (60*60*24)),
  20. diff1 = diff % (60*60*24),
  21. dh = parseInt(diff1 / (60*60)),
  22. diff2 = diff1 % (60*60),
  23. dm = parseInt(diff2 / 60),
  24. ds = parseInt(diff2 % 60);
  25. day_s = dd ? dd+'-' : '';
  26. target.innerHTML = day_s+a0(dh)+':'+a0(dm)+':'+a0(ds);
  27. }
  28. function a0(n) {
  29. if (n < 10) return '0'+n;
  30. return n;
  31. }
  32. full_update();
  33. setInterval(update, 1000);
  34. }