I have a small project, a stopwatch and what I want to do is:
When you click on "Reset Laps", the list of saved laps disappear
What I tried
1.
function removingLaps() {
laps.style.display = 'none'
}
and of course it works, but I have another function in the code that you will see from the snippet that limits the number of laps that can be saved to 5. if I just say display: none
, the li
elements are still existing in the DOM so the function doesn't work.
2.
function removingLaps() {
laps.remove()
}
but in this case unless you refresh the page, the ul
will be gone, so there would be no place to append the li
s.
3.
function removingLaps() {
let lapsToRemove = document.querySelectorAll('.lapsss')
lapsToRemove.remove()
}
but it says that is not a function. If I try with querySelector
instead of querySelectorAll
, it works, but it cancel the laps one by one. One click - one lap deleted.
I also tried to get the li
s by tag name, but it says that cannot set remove()
of null.
I really don't know what else to try.
The Snippet
let container = document.querySelector('.container')
let start = document.querySelector('.start')
let pause = document.querySelector('.pause')
let reset = document.querySelector('.reset')
let laps = document.querySelector('.laps')
let lapCrono = document.querySelector('.lapcrono')
let resetLaps = document.querySelector('.resetlaps')
let timer
let m = 0
let s = 0
let ms = 0
//START button **************
function runCrono(){
container.innerText = (m<10?'0'+m:m) + ':' + (s<10?'0'+s:s) + ':' + (ms<10?'0'+ms:ms)
ms++
if(ms == 100) {
ms = 0
s++
}
if(s == 60) {
s = 0
m++
}
}
start.addEventListener('click', ()=> {
if(!timer) {
timer = setInterval(runCrono, 10)
}
})
//Lap button *************+
function lap() {
let li = document.createElement('li')
if(timer) {
li.innerText = (m<10?'0'+m:m) + ' : ' + (s<10?'0'+s:s) + ' : ' + (ms<10?'0'+ms:ms)
laps.appendChild(li)
li.setAttribute('class', 'lapsss')
}
if(document.getElementsByTagName('li').length >= 5){
lapCrono.disabled = true
}
}
lapCrono.addEventListener('click', ()=> {
lap()
})
//PAUSE button ***************
pause.addEventListener('click', ()=> {
clearInterval(timer)
timer = false
})
//Reset Button ****************
reset.addEventListener('click', ()=> {
container.innerText = '00:00:00'
clearInterval(timer)
timer = false
})
//Reset Laps*******************
resetLaps.addEventListener('click', ()=> {
removingLaps()
lapCrono.disabled = false
})
function removingLaps() {
let lapsToRemove = document.getElementsByTagName('li')
lapsToRemove.remove()
}
* {
margin: 0;
padding: 0;
box-sizing: 0;
}
body, html {
background: url(speed.jpg);
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
background-position: center;
overflow: hidden;
}
.container {
display: flex;
justify-content: center;
width: 100%;
margin: auto;
margin-bottom: 3em;
border: 1px solid black;
font-size: 3em;
margin-top: 1em;
font-family: verdana;
}
.outcontainer {
border: 1px solid black;
width: 90vw;
margin: auto;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
top: 20vh;
left: 50vw;
transform: translate(-50%);
}
<div class="outcontainer">
<div class="container">00:00:00</div>
<div class="buttons">
<button class="start">Start</button>
<button class="lapcrono">Lap</button>
<button class="pause">Pause</button>
<button class="reset">Reset</button>
<button class="resetlaps">Reset Laps</button>
</div>
<ul class="laps">
</ul>
</div>
<script src="main.js"></script>