setInterval javascript does not work

Asked

Viewed 524 times

2

The following code works as the setTimeout function (happening only once and not repeating), I have tried several ways and could not, if anyone can help me, already thank.

    function girar(){           
        var bola = document.getElementById("ball");     
        window.xvel = 10;       
        setInterval(function(){                     
                    window.xvel = parseFloat(window.xvel) + parseFloat("10");                       
        }, 500);            
        bola.style.webkitTransform = "rotate(" + window.xvel + "deg)";      
     }

1 answer

4


You need to change the rotation within of callback of setInterval:

function girar(){           
    var bola = document.getElementById("ball");     
    window.xvel = 10;       
    setInterval(function(){                     
        window.xvel = parseFloat(window.xvel) + parseFloat("10");   
        bola.style.webkitTransform = "rotate(" + window.xvel + "deg)"; 
    }, 500);                      
}

Example with square ball

Browser other questions tagged

You are not signed in. Login or sign up in order to post.