I'm making an image slideshow, where 3 images have their CSS classes switched .onClick() so that they rotate position.
.flip-1 becomes .flip-2
.flip-2 becomes .flip-3
.flip-3 becomes .flip-1
Problem: my JQuery runs too fast, and I think recursively so every image ends up with the class .flip-1 in element inspect.
My JQuery is this:
$(document).ready(function () {
$('.ebook--flip').click(function() {
$('.flip-1').removeClass('flip-1').addClass('flip-2');
$('.flip-2').removeClass('flip-2').addClass('flip-3');
$('.flip-3').removeClass('flip-3').addClass('flip-1');
});
});
I was Googling around and think I could use delays, but is there a way to just run the remove/add class functions in sequence and stop at the end?
Once per click?
.removeClass()andaddClass()are instantaneous... And.delay()only has effect on jQuery animations. You probably have to usesetTimeout().$('.flip-x')lines to run once. One, then the other..ebook--flipon each click and then change the class to the next in series. This would prevent the issue of it being dependent on time and all happening at once.