You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB

  1. function getTimeRemaining(endtime){
  2. const total = Date.parse(endtime) - Date.parse(new Date());
  3. const seconds = Math.floor( (total/1000) % 60 );
  4. const minutes = Math.floor( (total/1000/60) % 60 );
  5. const hours = Math.floor( (total/(1000*60*60)) % 24 );
  6. const days = Math.floor( total/(1000*60*60*24) );
  7. return {
  8. total,
  9. days,
  10. hours,
  11. minutes,
  12. seconds
  13. };
  14. }
  15. function initializeClock(id, endtime) {
  16. const clock = document.getElementById(id);
  17. const daysSpan = clock.querySelector('.days');
  18. const hoursSpan = clock.querySelector('.hours');
  19. const minutesSpan = clock.querySelector('.minutes');
  20. const secondsSpan = clock.querySelector('.seconds');
  21. function updateClock() {
  22. const t = getTimeRemaining(endtime);
  23. if (t.days < 0) {
  24. clearInterval(timeinterval);
  25. $('#clock-card').hide();
  26. }
  27. daysSpan.innerHTML = t.days;
  28. hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
  29. minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
  30. secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
  31. if (t.total <= 0) {
  32. clearInterval(timeinterval);
  33. }
  34. }
  35. updateClock(); // run function once at first to avoid delay
  36. var timeinterval = setInterval(updateClock, 1000);
  37. }
  38. initializeClock('clockdiv', deadline);
  39. $('#trip-name').text(tripName);