0

I have an animation of a car driving back and forth across the screen and have figured out how to flip the image when it reaches the right side of the screen and then again when it reaches the left side. However, I need to loop this JS event so the animation constantly shows the car driving in the correct 'direction.' The code below should give you an idea of what I'm working with, so if anyone could be so kind as to help this beginner figure out how to make it loop infinitely, I would greatly appreciate it.

*tl/dr

Basically I need the code below to loop infinitely.

var carorange = $('.org-car');
    setTimeout(function () {
        carorange.addClass('flipimg');
    }, 5000);
    setTimeout(function () {
        carorange.removeClass('flipimg');
    }, 10000);
4

1 に答える 1

3

Use setInterval instead of setTimeout:

var carorange = $('.org-car');
setInterval(function () {
    carorange.toggleClass('flipimg');
}, 5000);

setInterval has a nother big advantage: You can stop the interval. So if you assign the interval to a variable, like var myTimer = setInterval..., you can stop it at any time using clearInterval(myTimer); :)

于 2013-06-23T17:46:37.767 に答える