-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Silvyjson/play-pause-function
Created a Restart logic
- Loading branch information
Showing
5 changed files
with
159 additions
and
100 deletions.
There are no files selected for viewing
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,185 @@ | ||
const listElement = document.getElementById("menu"); | ||
const minuteInput = document.getElementById('minuteInput'); | ||
const secondInput = document.getElementById('secondInput'); | ||
const startButton = document.getElementById('startButton'); | ||
const minuteInput = document.getElementById("minuteInput"); | ||
const secondInput = document.getElementById("secondInput"); | ||
const startButton = document.getElementById("startButton"); | ||
const body = document.body; | ||
const statusBarImg1 = document.getElementById("status-bar--img1"); | ||
const statusBarImg2 = document.getElementById("status-bar--img2"); | ||
let intervalValue = null; | ||
let remainingSeconds = 0; | ||
const listElement = document.getElementById("menu"); | ||
const pauseIcon = document.getElementById("pause"); | ||
|
||
const toggleList = () => { | ||
|
||
if (listElement.style.display === "block") { | ||
listElement.style.display = "none"; | ||
} | ||
|
||
else { | ||
listElement.style.display = "block"; | ||
} | ||
} | ||
|
||
function hideMenuList() { | ||
if (listElement.style.display === "block") { | ||
listElement.style.display = "none"; | ||
} else { | ||
listElement.style.display = "block"; | ||
} | ||
}; | ||
|
||
document.body.addEventListener('click', function (event) { | ||
function hideMenuList() { | ||
listElement.style.display = "none"; | ||
} | ||
|
||
if (!event.target.closest('.menu') && !event.target.closest('.menu-list')) { | ||
hideMenuList(); | ||
} | ||
document.body.addEventListener("click", function (event) { | ||
if (!event.target.closest(".menu") && !event.target.closest(".menu-list")) { | ||
hideMenuList(); | ||
} | ||
}); | ||
|
||
|
||
function toggleMode() { | ||
|
||
const body = document.body; | ||
body.classList.toggle('dark_mode'); | ||
|
||
listElement.innerHTML = 'Switch to light mode' | ||
|
||
const statusBarImg1 = document.getElementById("status-bar--img1"); | ||
const statusBarImg2 = document.getElementById("status-bar--img2"); | ||
|
||
if (statusBarImg1.style.display === "none") { | ||
statusBarImg1.style.display = "block"; | ||
statusBarImg2.style.display = "none"; | ||
} | ||
|
||
else { | ||
statusBarImg1.style.display = "none"; | ||
statusBarImg2.style.display = "block"; | ||
} | ||
|
||
hideMenuList(); | ||
body.classList.toggle("dark_mode"); | ||
|
||
if (body.classList.contains("dark_mode")) { | ||
listElement.innerHTML = "Switch to light mode"; | ||
localStorage.setItem("currentMode", "darkMode"); | ||
} else { | ||
listElement.innerHTML = "Switch to dark mode"; | ||
localStorage.setItem("currentMode", "lightMode"); | ||
} | ||
|
||
if (statusBarImg1.style.display === "none") { | ||
statusBarImg1.style.display = "block"; | ||
statusBarImg2.style.display = "none"; | ||
} else { | ||
statusBarImg1.style.display = "none"; | ||
statusBarImg2.style.display = "block"; | ||
} | ||
|
||
hideMenuList(); | ||
} | ||
|
||
function toggleStart() { | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const storedMode = localStorage.getItem("currentMode"); | ||
|
||
if (storedMode === "darkMode") { | ||
body.classList.toggle("dark_mode"); | ||
listElement.innerHTML = "Switch to light mode"; | ||
statusBarImg1.style.display = "none"; | ||
statusBarImg2.style.display = "block"; | ||
} else { | ||
listElement.innerHTML = "Switch to dark mode"; | ||
statusBarImg1.style.display = "block"; | ||
statusBarImg2.style.display = "none"; | ||
} | ||
}); | ||
|
||
toggleRestart(); | ||
function toggleStart() { | ||
toggleDelete(); | ||
|
||
const minutes = parseInt(minuteInput.value) || 0; | ||
const seconds = parseInt(secondInput.value) || 0; | ||
totalTimer(); | ||
|
||
const totalSeconds = minutes * 60 + seconds; | ||
updateTimer(totalSeconds); | ||
playTimer(); | ||
} | ||
|
||
let remainingSeconds = totalSeconds; | ||
const totalTimer = () => { | ||
const minutes = parseInt(minuteInput.value) || 0; | ||
const seconds = parseInt(secondInput.value) || 0; | ||
|
||
if (totalSeconds > 0) { | ||
let totalSeconds = minutes * 60 + seconds; | ||
|
||
intervalValue = setInterval(() => { | ||
remainingSeconds--; | ||
remainingSeconds = totalSeconds; | ||
|
||
updateTimer(remainingSeconds); | ||
if (remainingSeconds === 0) { | ||
clearInterval(intervalValue); | ||
} | ||
}, 1000); | ||
} | ||
} | ||
updateTimer(totalSeconds); | ||
}; | ||
|
||
const updateTimer = (seconds = 0) => { | ||
const timeTextElement = document.getElementById("time-text"); | ||
|
||
const timeTextElement = document.getElementById("time-text"); | ||
|
||
timeTextElement.innerHTML = `${Math.floor(seconds / 60)}:${seconds % 60}`; | ||
timeTextElement.innerHTML = `${Math.floor(seconds / 60)}:${seconds % 60}`; | ||
}; | ||
|
||
const toggleDelete = () => { | ||
const timerStart = document.getElementById("timer--start"); | ||
const timerCircle = document.getElementById("timer-count"); | ||
|
||
const toggleRestart = () => { | ||
if (timerStart.style.display === "none") { | ||
timerStart.style.display = "block"; | ||
timerCircle.style.display = "none"; | ||
} else { | ||
timerStart.style.display = "none"; | ||
timerCircle.style.display = "block"; | ||
} | ||
|
||
const timerStart = document.getElementById("timer--start"); | ||
const timerCircle = document.getElementById("timer-count"); | ||
clearInterval(intervalValue); | ||
}; | ||
|
||
if (timerStart.style.display === "none") { | ||
timerStart.style.display = "block"; | ||
timerCircle.style.display = "none"; | ||
} else { | ||
timerStart.style.display = "none"; | ||
timerCircle.style.display = "block"; | ||
} | ||
const digitInput = (event, inputElement) => { | ||
const allowedKeys = [ | ||
"0", | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"5", | ||
"6", | ||
"7", | ||
"8", | ||
"9", | ||
"Backspace", | ||
"ArrowLeft", | ||
"ArrowRight", | ||
]; | ||
const exceptionKeys = ["Backspace", "ArrowLeft", "ArrowRight"]; | ||
|
||
if ( | ||
!allowedKeys.includes(event.key) || | ||
(!exceptionKeys.includes(event.key) && inputElement.value.length === 2) | ||
) { | ||
event.preventDefault(); | ||
} | ||
if (minuteInput.value.trim() !== "" || secondInput.value.trim() !== "") { | ||
startButton.removeAttribute("disabled"); | ||
} else { | ||
startButton.setAttribute("disabled", "true"); | ||
} | ||
}; | ||
|
||
clearInterval(intervalValue); | ||
const inputHandler = (inputElement) => { | ||
inputElement.addEventListener("keydown", function (event) { | ||
digitInput(event, inputElement); | ||
}); | ||
|
||
} | ||
inputElement.addEventListener("input", function (event) { | ||
digitInput(event, inputElement); | ||
}); | ||
}; | ||
|
||
const digitInput = (event, inputElement) => { | ||
const allowedKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Backspace', 'ArrowLeft', 'ArrowRight']; | ||
const exceptionKeys = ["Backspace", "ArrowLeft", "ArrowRight"]; | ||
inputHandler(minuteInput); | ||
inputHandler(secondInput); | ||
|
||
if (!allowedKeys.includes(event.key) || (!exceptionKeys.includes(event.key) && inputElement.value.length === 2)) { | ||
event.preventDefault(); | ||
} | ||
const playTimer = () => { | ||
if (remainingSeconds > 0) { | ||
intervalValue = setInterval(() => { | ||
remainingSeconds--; | ||
|
||
updateTimer(remainingSeconds); | ||
if (remainingSeconds === 0) { | ||
pauseTimer(); | ||
} | ||
}, 1000); | ||
} | ||
}; | ||
|
||
if (minuteInput.value.trim() !== '' || secondInput.value.trim() !== '') { | ||
startButton.removeAttribute('disabled'); | ||
} | ||
const pauseTimer = () => { | ||
clearInterval(intervalValue); | ||
intervalValue = null; | ||
}; | ||
|
||
else { | ||
startButton.setAttribute('disabled', 'true'); | ||
} | ||
function togglePlay() { | ||
if (pauseIcon.src.endsWith("pause-icon.png")) { | ||
pauseIcon.src = "./image/play-icon.png"; | ||
pauseTimer(); | ||
} else { | ||
pauseIcon.src = "./image/pause-icon.png"; | ||
playTimer(); | ||
} | ||
} | ||
|
||
const inputHandler = (inputElement) => { | ||
inputElement.addEventListener('keydown', function (event) { | ||
digitInput(event, inputElement); | ||
}); | ||
function toggleRefresh() { | ||
pauseTimer(); | ||
|
||
inputElement.addEventListener('input', function (event) { | ||
digitInput(event, inputElement); | ||
}); | ||
totalTimer(); | ||
pauseIcon.src = "./image/pause-icon.png"; | ||
playTimer(); | ||
} | ||
|
||
inputHandler(minuteInput); | ||
inputHandler(secondInput); |