+ 9
[Solved]Why does the speed of increment in the code fluctuate??
I wanted to make a code to Increment the variable after a fixed time interval.. But in my code the variable is incremented at given speed for sometime at the start.. But if user clicks the button rapidly the variable is then incremented rapidly even when the user isn't pressing the button.. 😕 Also I don't know of i should post this in Q&A section or my personal post... 😅 Any help is appreciated!! https://code.sololearn.com/W4H9Lfmsfj4h/?ref=app
3 Respostas
+ 3
/*hi $hardul,
its because you are creating new loop everytime you click. So if you dont want that to happen then clear the interval before starting new */
var i=0;
var myInterval;
function auto()
{
if(myInterval !== undefined){
clearInterval(myInterval);
}
document.getElementById("out").innerHTML=i;
myInterval = setInterval(auto1,3000);
}
function auto1()
{
i=i+1;
document.getElementById("out").innerHTML=i;
}
+ 4
Pressing more time on button would make more setInterval calls, multiple timers update of course make the increment run rapidly.