I made an auto-slide with requestAnimationFrame (RFA), I have 2 slides appearing after each other, not appear the same time. This works fine only when I always at current tab. If I click another tab then come back the current tab, it seems the slides is mixed, it appear together. It seems that the animation is paused at the time I clicking other tab. I have to wait for a few second then It will work fine again. I used browser Chrome, Firefox and Edge, all have same issue. Before using RFA, I have tried the setInterval but then saw setInterval is not as good as RFA so I use RFA. Can you give me any advice for this issue? You can see the demo here: https://repl.it/repls/SelfassuredOverjoyedGlitch#index.html
const slide_1 = document.getElementById("top-slide-1");
const slide_2 = document.getElementById("top-slide-2");
var startTime;
// SLIDE 1
const $behindImage = $("#behind-img");
const $frontImg = $("#front-img");
const $topSlideTitle = $("#top-slide-1__title");
const $topSlideTemplate = $("#top-slide-1__template");
const $topSlideResponsive = $("#top-slide-1__responsive");
const $topSlideButton = $("#top-slide-1__button");
var setTimeoutSlide1;
var setTimeoutSlide2;
function top_slide_1(){
slide_1.style.zIndex = 1000;
slide_2.style.zIndex = 0;
startTime = new Date().getTime();
$behindImage.animate({
"right": 370
}, 500, "swing", function () {
$frontImg.animate({
"right": 230
}, 500, "swing", function (){
$topSlideTitle.animate({
"top": 65
}, 300, "swing", function (){
$topSlideTemplate.animate({
"left": 205
}, 300, "swing", function(){
$topSlideResponsive.animate({
"left": 205
}, 300, "swing", function (){
$topSlideButton.animate({
"top": 260
}, 300, "swing", function(){
$topSlideButton.animate({
"top": 340
}, 50, "swing", function (){
$topSlideButton.animate({
"top": 300
}, 100, "swing", () => {
setTimeoutSlide1 = setTimeout(() => {
$behindImage.animate({
"right": "-100%"
}, 300);
$frontImg.animate({
"right": "-100%"
}, 300);
$topSlideTitle.animate({
"top": "-100%"
}, 300);
$topSlideTemplate.animate({
"left": "-100%"
}, 300);
$topSlideResponsive.animate({
"left": "100%"
}, 300);
$topSlideButton.animate({
"top": "100%"
}, 300)
}, 3000)
})
})
})
})
})
})
})
})
}
// END SLIDE 1
// SLIDE 2
const $behindImage2 = $("#behind-img-2");
const $frontImg2 = $("#front-img-2");
const $topSlide2Title = $("#top-slide-2__title");
const $topSlide2Color = $("#top-slide-2__color");
const $topSlide2Structure = $("#top-slide-2__structure");
const $topSlide2Customize = $("#top-slide-2__customize");
const $topSlide2Everything = $("#top-slide-2__everything");
function top_slide_2(){
slide_1.style.zIndex = 0;
slide_2.style.zIndex = 1000;
startTime = new Date().getTime();
$behindImage2.animate({
right: 560
}, 300, "swing", function (){
$frontImg2.animate({
bottom: 30
}, 300, "swing", function (){
$topSlide2Title.animate({
top: 100
}, 300, "swing", function(){
$topSlide2Color.animate({
top: 190
}, 300);
$topSlide2Structure.animate({
top: 235
}, 500);
$topSlide2Customize.animate({
top: 280
}, 800, "swing", function (){
$topSlide2Everything.animate({
top: 350
}, 300, "swing", function (){
setTimeoutSlide2 = setTimeout(() => {
$behindImage2.animate({right: "-100%"}, 200);
$frontImg2.animate({bottom: "-100%"}, 200);
$topSlide2Title.animate({top: "-100%"}, 200);
$topSlide2Color.animate({top: "-100%"}, 200);
$topSlide2Structure.animate({top: "-100%"}, 200);
$topSlide2Customize.animate({top: "-100%"}, 200);
$topSlide2Everything.animate({top: "100%"}, 200);
}, 3700)
})
})
})
})
})
}
top_slide_1(); // default when page load
let counter = 1;
let runTopSlide = function (){
let currentTime = new Date().getTime();
let passedTime = currentTime - startTime;
if (passedTime >= 6200){
console.log(passedTime);
counter += 1;
if (counter > 2) {counter = 1};
if (counter === 1) {
top_slide_1();
} else top_slide_2();
}
window.requestAnimationFrame(runTopSlide)
}
runTopSlide();