|
| 1 | +/** |
| 2 | + In this week 's project you'll be making a Pomodoro Clock! |
| 3 | + A user can specify how many minutes the timer should be set, and with a click on the play button it starts counting down!If the user wants to pause the timer, they can do so by clicking the pause button. |
| 4 | +
|
| 5 | + If the timer is running, the user can 't change the session length anymore |
| 6 | + Use at least 3 functions |
| 7 | + Display minutes and seconds |
| 8 | + If the timer finishes the timer should be replaced by the message: Time 's up! |
| 9 | + * |
| 10 | + */ |
| 11 | +'use strict'; |
| 12 | +const up = document.getElementById('up'); |
| 13 | +const down = document.getElementById('down'); |
| 14 | +const play = document.getElementById('play'); |
| 15 | +const pause = document.getElementById('pause'); |
| 16 | +const countDown = document.getElementById('countdown'); |
| 17 | +const workMin = document.getElementById('workSession'); |
| 18 | +const stop = document.getElementById('stop'); |
| 19 | + |
| 20 | +let counter = 0; |
| 21 | +let seconds = 0; |
| 22 | +let remaining = 0; |
| 23 | +let isPaused = true; |
| 24 | + |
| 25 | +// Event Listener for Play and stop buttons |
| 26 | +play.addEventListener('click', () => { |
| 27 | + clearInterval(counter); |
| 28 | + if (isPaused) { |
| 29 | + startCountdown(); |
| 30 | + play.innerHTML = `<i class="fas fa-pause"></i>`; |
| 31 | + isPaused = false; |
| 32 | + } else { |
| 33 | + remaining = seconds; |
| 34 | + play.innerHTML = `<i class="fas fa-play"></i>`; |
| 35 | + isPaused = true; |
| 36 | + } |
| 37 | +}); |
| 38 | +stop.addEventListener('click', () => { |
| 39 | + clearInterval(counter); |
| 40 | + seconds = workMin.textContent * 60; |
| 41 | + counter = 0; |
| 42 | + remaining = 0; |
| 43 | + isPaused = true; |
| 44 | + play.innerHTML = `<i class="fas fa-play"></i>`; |
| 45 | + displayTimeLeft(seconds); |
| 46 | +}); |
| 47 | + |
| 48 | +// Main Functions |
| 49 | + |
| 50 | +function timer() { |
| 51 | + seconds--; |
| 52 | + if (seconds < 0) { |
| 53 | + clearInterval(counter); |
| 54 | + seconds = workMin.textContent * 60; |
| 55 | + countdown = setInterval(timer, 1000); |
| 56 | + return; |
| 57 | + } |
| 58 | + displayTimeLeft(seconds); // Keep updating display |
| 59 | +} |
| 60 | + |
| 61 | +function startCountdown() { |
| 62 | + if (remaining != 0) { |
| 63 | + seconds = remaining; |
| 64 | + } else { |
| 65 | + seconds = workMin.textContent * 60; |
| 66 | + } |
| 67 | + counter = setInterval(timer, 1000); |
| 68 | +} |
| 69 | + |
| 70 | +function displayTimeLeft(seconds) { |
| 71 | + const minutes = Math.floor(seconds / 60); |
| 72 | + const remainderSeconds = seconds % 60; |
| 73 | + countDown.textContent = `${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`; |
| 74 | +} |
| 75 | + |
| 76 | +// update work times |
| 77 | +up.addEventListener('click', () => { |
| 78 | + let x = parseInt(countDown.textContent); |
| 79 | + if (x < 60) { |
| 80 | + countDown.textContent = x + 1; |
| 81 | + workMin.textContent = countDown.textContent; |
| 82 | + } |
| 83 | +}); |
| 84 | +down.addEventListener('click', () => { |
| 85 | + let x = parseInt(countDown.textContent); |
| 86 | + if (x > 5) { |
| 87 | + countDown.textContent = x - 1; |
| 88 | + workMin.textContent = countDown.textContent; |
| 89 | + } |
| 90 | +}); |
0 commit comments