timer.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // timer
  2. function getTimeRemaining(endtime) {
  3. let t = Date.parse(endtime) - Date.parse(new Date());
  4. let seconds = Math.floor((t / 1000) % 60);
  5. let minutes = Math.floor((t / 1000 / 60) % 60);
  6. return {
  7. 'total': t,
  8. 'minutes': minutes,
  9. 'seconds': seconds
  10. };
  11. }
  12. function initializeClock(id, endtime) {
  13. let clock = document.getElementById(id);
  14. let minutesSpan = clock.querySelector('.minutes');
  15. let secondsSpan = clock.querySelector('.seconds');
  16. function updateClock() {
  17. let t = getTimeRemaining(endtime);
  18. minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
  19. secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
  20. submitButton.onclick = (e) => {
  21. e.preventDefault();
  22. clearInterval(timeinterval);
  23. clock.innerHTML = '';
  24. showResults();
  25. }
  26. if (t.total <= 0) {
  27. clearInterval(timeinterval);
  28. clock.innerHTML = '';
  29. showResults();
  30. }
  31. }
  32. updateClock();
  33. const timeinterval = setInterval(updateClock, 1000);
  34. return timeinterval;
  35. }
  36. let deadline = new Date(Date.parse(new Date()) + 60 * 1000);
  37. //initializeClock('countdown', deadline);
  38. const btnStart = document.getElementById('btnStart');
  39. btnStart.onclick = (e) => {
  40. e.preventDefault();
  41. e.stopPropagation();
  42. initializeClock('countdown', deadline);
  43. buildQuiz();
  44. localStorage.setItem('timeinterval', initializeClock('countdown', deadline));
  45. }
  46. const btnGradeMe = document.getElementById('submit');
  47. window.onload = function () {
  48. localStorage.removeItem('answers')
  49. }