Advertisement
Guest User

Untitled

a guest
May 25th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. function pad(n, len) { // leading 0's
  2. var s = n.toString();
  3. return (new Array( (len - s.length + 1) ).join('0')) + s;
  4. };
  5.  
  6. function countDown() {
  7. var now = new Date();
  8. var targetDay = 4; // Thursday (4) is the target day
  9. var target = 0; // Hour of the day to cutoff (0 for midnight)
  10. var days = 0, hrs = 0, mins = 0, secs = 0;
  11.  
  12. //--> Day logic
  13. if(now.getDay() < targetDay - 1) {
  14. days = targetDay - now.getDay() - 1;
  15. }
  16. else if(now.getDay() > targetDay) {
  17. days = 8 - now.getDay() + targetDay;
  18. }
  19. //<-- Day logic
  20.  
  21. // Countdown logic
  22. if (now.getHours() < target) {
  23. hrs = (target - 1) - now.getHours();
  24. if (hrs < 0) hrs = 0;
  25. mins = 59 - now.getMinutes();
  26. if (mins < 0) mins = 0;
  27. secs = 59 - now.getSeconds();
  28. if (secs < 0) secs = 0;
  29. }
  30. else if (now.getHours() > target) {
  31. hrs = 24 - now.getHours() + (target - 1);
  32. if (hrs < 0) hrs = 0;
  33. mins = 59 - now.getMinutes();
  34. if (mins < 0) mins = 0;
  35. secs = 59 - now.getSeconds();
  36. if (secs < 0) secs = 0;
  37. }
  38. else { // It's time!
  39. document.getElementById('countdownTimer').innerHTML = "It's time!";
  40. }
  41. }
  42.  
  43. // Run immediately to get rid of delay
  44. countDown();
  45.  
  46. // Run every second to update time remaining
  47. if (document.getElementById('countdownTimer')) {
  48. var timerRunning = setInterval(
  49. function() {
  50. countDown();
  51. }, 1000
  52. );
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement