I ran into a problem with my Counter on website which Im building. I made Counter and it's working perfect, the problem is that it works constantly, not when scrolling to the section where it is located. So I want just make it to start count when it comes section where he is. Hope you guys understand me, and I would be grateful to anyone who wants to help me. Here is my JavaScript code:
var pacijenti = setInterval(pacijenata, 300);
var procenat = setInterval(procenatIzlecenosti, 60);
var klinika = setInterval(klinike, 400);
let count1 = 0;
let count2 = 0;
let count3 = 0;
function pacijenata() {
count1 = count1 += 1000;
document.querySelector("#number1").innerHTML = count1 + "+";
if( count1 === 10000 ) {
clearInterval(pacijenti);
}
}
function procenatIzlecenosti() {
count2++;
document.querySelector("#number2").innerHTML = count2 + "%";
if( count2 === 70 ) {
clearInterval(procenat);
}
}
function klinike() {
count3++;
document.querySelector("#number3").innerHTML = count3;
if( count3 === 4 ) {
clearInterval(klinika);
}
}
<section id="brojac">
<div class="counterContainer">
<ul>
<li>
<i class="fas fa-hospital-user"></i>
<p id="number1" class="number">10000</p>
<p>Pacijenata</p>
</li>
<li>
<i class="fas fa-universal-access"></i>
<p id="number2" class="number">70</p>
<p>Procenat izlečenosti</p>
</li>
<li>
<i class="fas fa-clinic-medical"></i>
<p id="number3" class="number">4</p>
<p>Klinike</p>
</li>
</ul>
</div>
</section>