So I have been trying to make like a timer type thing to count from 0 to 5 (one number up every sec, like normal timer) then stopping at 5 but It’s not stopping at 5. What did I do wrong?
var licz = 0; function timer() { window.setInterval( function() { document.getElementById('timer_r').innerHTML = licz; licz++; if (licz >= 5) { window.clearInterval(timer); } } , 1000 ); }
<input type="button" id="timer" on value="timer" onclick="timer();"> <p>Odliczanie: <span id='timer_r'> _ </span></p>
Advertisement
Answer
What you need to be doing is complete your conditional like:-
<script> var licz = 0; function timer() { window.setInterval( function(){ document.getElementById('timer_r').innerHTML = licz; if (licz < 5) { licz++; } else { window.clearInterval(timer); } }, 1000); } </script> <br> <input type = "button" id = "timer" on value = "timer" onclick="timer();"> <p>Odliczanie: <span id = 'timer_r'> _ </span></p>