0

I know I can use animation and @keyframes in CSS but I want to have this problem solved below. I want to pause this animation later using clearInterval (<- but thats not my question).

What the problem is, this animation starts after one second. What can I do to make animation working immediately after refreshing the page? (- JSFIDDLE -)

<div id="myID"></div>
-----------------------
var i = 0;
var thesquare = document.getElementById('myID');
function FunRotate()
{
    switch(i)
    {
        case 0:
        thesquare.style.transform = 'rotate(20deg)';
        i++;
        break;

        default:
        thesquare.style.transform = 'rotate(-20deg)';
        i=0;
        break;
    }
}
setInterval(FunRotate, 1000);

EDIT: No it isn't a duplicate. I want to have the first step of animation immediately.

Kardaw
  • 371
  • 2
  • 14

2 Answers2

0

Add FunRotate(); once before your setInterval(FunRotate, 1000); ...

weeger
  • 429
  • 2
  • 9
0

Invoke it immediately and set a timeOut for the next one at the end of the call:

function FunRotate()
{
    switch(i)
    {
    case 0:
    thesquare.style.transform = 'rotate(20deg)';
    i++;
    break;

    default:
    thesquare.style.transform = 'rotate(-20deg)';
    i=0;
    break;

    setTimeout(FunRotate, 1000);
}

FunRotate();
tymeJV
  • 103,943
  • 14
  • 161
  • 157