Hamid
Hamid

Reputation: 97

creating animation in JS , but if condition doesn't run

I have this function which I put it in an interval. but When the value of i reaches 350 my function doesn't execute if statement and my div continues to growing up and doesn't run clearInterval.

    var timerA = setInterval(spinnerA, 10);
    
    var i = 150;
    var d = 0;
    function spinnerA() {
       if(i ==350) {
           if(i == 151) {
               clearInterval(timerA)
           }else{
               i-=3;
               div.style.height = i + "px";
               div.style.width = i + "px";
           }
       } else {
           i+=3;
           div.style.height = i + "px";
           div.style.width = i + "px";
       }
    }

Upvotes: 0

Views: 107

Answers (1)

CoderInUi
CoderInUi

Reputation: 136

Your condition is not satisfying, in order for your loop to run 350th time, change the condition to i >= 350.

If we go by your logic, it will never reach 350 because the moment i reaches closer to 350 (which in your case is 348) the outer else statement will increment it to 351 and hence loop will never enter outer if statement.

Upvotes: 1

Related Questions