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?
JavaScript
x
14
14
1
var licz = 0;
2
3
function timer() {
4
window.setInterval(
5
function() {
6
document.getElementById('timer_r').innerHTML = licz;
7
licz++;
8
if (licz >= 5) {
9
window.clearInterval(timer);
10
}
11
}
12
, 1000
13
);
14
}
JavaScript
1
2
1
<input type="button" id="timer" on value="timer" onclick="timer();">
2
<p>Odliczanie: <span id='timer_r'> _ </span></p>
Advertisement
Answer
What you need to be doing is complete your conditional like:-
JavaScript
1
22
22
1
<script>
2
3
var licz = 0;
4
function timer() {
5
window.setInterval(
6
function(){
7
document.getElementById('timer_r').innerHTML = licz;
8
9
if (licz < 5) {
10
licz++;
11
} else {
12
window.clearInterval(timer);
13
}
14
15
16
}, 1000);
17
}
18
19
</script>
20
<br>
21
<input type = "button" id = "timer" on value = "timer" onclick="timer();">
22
<p>Odliczanie: <span id = 'timer_r'> _ </span></p>