Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KajizukaTaichi committed May 21, 2024
1 parent ec864f5 commit 33d523f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,43 @@ function stopAlarm() {
alarmSound.currentTime = 0;
}

function resetTimer() {
clearInterval(timer);
timeInput.style.pointerEvents = 'auto';
timerDisplay.textContent = 'Timer stopped';
}

document.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
if (event.code === 'Enter') {
let time = parseInt(timeInput.value, 10);
if (isNaN(time) || time <= 0) {
timerDisplay.textContent = 'Please enter valid number';
return;
}

timeInput.blur();
timeInput.style.pointerEvents = 'none';
timerDisplay.textContent = `Set time is ${time} second`;
clearInterval(timer);

timer = setInterval(() => {
if (time <= 0) {
stopAlarm();
clearInterval(timer);
alarmSound.play();
timeInput.style.pointerEvents = 'auto';
clearInterval(timer);
timerDisplay.textContent = 'It became the set time!';
} else {
time--;
timeInput.value = time;
}
}, 1000);
} else if (event.code === 'Space') {
event.preventDefault();
stopAlarm();
} else if (event.code === 'Escape') {
event.preventDefault();
resetTimer();
}
});

timerDisplay.addEventListener("click", stopAlarm)
timerDisplay.addEventListener("click", stopAlarm);
timeInput.addEventListener("click", resetTimer);

0 comments on commit 33d523f

Please sign in to comment.