Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // *************************************************
  2. // Experimental javascript countdown timer (Splarka)
  3. // Version 0.0.3
  4. // *************************************************
  5. //
  6. // Usage example:
  7. // <span class="countdown" style="display:none;">
  8. // Only <span class="countdowndate">January 01 2007 00:00:00 PST</span> until New years.
  9. // </span>
  10. // <span class="nocountdown">Javascript disabled.</span>
  11.  
  12. function updatetimer(i) {
  13. var now = new Date();
  14. var then = timers[i].eventdate;
  15. var diff = count=Math.floor((then.getTime()-now.getTime())/1000);
  16.  
  17. // catch bad date strings
  18. if(isNaN(diff)) {
  19. timers[i].firstChild.nodeValue = '** ' + timers[i].eventdate + ' **' ;
  20. return;
  21. }
  22.  
  23. // determine plus/minus
  24. if(diff<0) {
  25. diff = -diff;
  26. var tpm = 'T plus ';
  27. } else {
  28. var tpm = 'T minus ';
  29. }
  30.  
  31. // calcuate the diff
  32. var left = (diff%60) + ' seconds';
  33. diff=Math.floor(diff/60);
  34. if(diff > 0) left = (diff%60) + ' minutes ' + left;
  35. diff=Math.floor(diff/60);
  36. if(diff > 0) left = (diff%24) + ' hours ' + left;
  37. diff=Math.floor(diff/24);
  38. if(diff > 0) left = diff + ' days ' + left
  39. timers[i].firstChild.nodeValue = tpm + left;
  40.  
  41. // a setInterval() is more efficient, but calling setTimeout()
  42. // makes errors break the script rather than infinitely recurse
  43. timeouts[i] = setTimeout('updatetimer(' + i + ')',1000);
  44. }
  45.  
  46. function checktimers() {
  47. //hide 'nocountdown' and show 'countdown'
  48. var nocountdowns = getElementsByClassName(document, 'span', 'nocountdown');
  49. for(var i in nocountdowns) nocountdowns[i].style.display = 'none'
  50. var countdowns = getElementsByClassName(document, 'span', 'countdown');
  51. for(var i in countdowns) countdowns[i].style.display = 'inline'
  52.  
  53. //set up global objects timers and timeouts.
  54. timers = getElementsByClassName(document, 'span', 'countdowndate'); //global
  55. timeouts = new Array(); // generic holder for the timeouts, global
  56. if(timers.length == 0) return;
  57. for(var i in timers) {
  58. timers[i].eventdate = new Date(timers[i].firstChild.nodeValue);
  59. updatetimer(i); //start it up
  60. }
  61. }
  62. addOnloadHook(checktimers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement