Просто поместите counter++
в конец вашей функции:
showAnime = () => {
let counter = 0;
setInterval(() => {
/* */
} else if (counter === 5) {
console.log(counter);
this.setState({
animateRightSix: "animated fadeInRightBig delay-six"
});
} else if (counter === 6) {
counter = 0;
}
counter++;
}, 7000);
};
Вы можете значительно сократить свой код, сохранив каждое предложение в массиве, а затем выбрав из него текущее предложение.
И, чтобы автоматически переключаться между предложениями, вы можете использовать модуль %
вместо установки счетчика на 0:
showAnime = () => {
let counter = 0;
setInterval(() => {
const sentences = [
"animated fadeInRightBig delay-one",
"animated fadeInRightBig delay-two",
"animated fadeInLeftBig delay-three",
"animated fadeInRightBig delay-five",
"animated fadeInRightBig delay-six"
]
this.setState({
animateRightOne: sentences[counter % 6]
});
counter++;
}, 7000);
};
Еще короче:
showAnime = () => {
let counter = 0;
setInterval(() => {
const sentences = [["one", 'Right'], ["two", 'Right'], ["three", 'Left'], ["five", 'Right'], ["six", 'Right'] ]
const [num, side] = sentences[counter % 6]
this.setState({
animateRightOne: `animated fadeIn${side}Big delay-${num}`
});
counter++;
}, 7000);
};