Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:57:10+00:00 2026-06-17T09:57:10+00:00

I’ve got a simple script that fades in and out some images. It works

  • 0

I’ve got a simple script that fades in and out some images. It works perfectly fine, before I switch to another tab and then switch back. After switching back seems that interval that runs animation doesn’t get cleared and animateFading function keeps running.

Why is it happening, how does tab switching affects script? And how could I make it work properly? thx!

The real code:

var img0 = document.getElementById("image0");
var img1 = document.getElementById("image1");
var img2 = document.getElementById("image2");

var imgs = [img0,img1,img2];

var intervalFading;
var intervalAnimate;

var imgThis = 0;
var imgNext = 1;

var fadeIn = 0;
var fadeOut = 1;

var start;



function animateFading() {
    fadeIn+=0.1;
    fadeOut-=0.1;
    imgs[imgThis].style.opacity = fadeOut;
    imgs[imgNext].style.opacity = fadeIn;

    if (fadeIn >= 1) {
        clearInterval(intervalAnimate);

        if (imgNext < 2) {
            imgNext++;
        } else {
            imgNext = 0;
        }

        if (imgThis < 2) {
            imgThis++;
        } else {
            imgThis = 0;
        }

        fadeIn = 0;
        fadeOut = 1;
    }

}

function fading() {
    intervalAnimate = setInterval(animateFading,50);
}

function startFading() {
    start = setInterval(fading, 3000);
}

window.addEventListener('load', startFading);

Solution:

var startedFading = false;

function startFading() {
    if (!startedFading) {
        startedFading = true;
        startFading = setInterval(fading, 5000);
        }
}

function stopFading() {
    startedFading = false;
        clearInterval(startFading);
}


window.addEventListener('focus', startFading);      
window.addEventListener('blur', stopFading);

The boolean is for Chrome, as it fires twice

window.addEventListener('focus', startFading);

&

window.addEventListener('blur', stopFading);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:57:11+00:00Added an answer on June 17, 2026 at 9:57 am

    Most browsers have a feature that redirects the memory usage capacity to the only active tab (currently being viewed). Thus, the setInterval and setTimeout timings are affected when you switch the current tab (if you’re using Google Chrome, you can see the memory usage of each tab by pressing Shift+Esc. The current tab will use up to 25%, whereas the rest don’t pass of the 5%)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.