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 9209195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:47:05+00:00 2026-06-18T00:47:05+00:00

So I have been recently developing a site, The problem is the backgrounds for

  • 0

So I have been recently developing a site, The problem is the backgrounds for each page are images, and as a result, on slower connections (which is the case of some of the target audience) the images load progressivly as they are downloaded, to resolve this I am trying to make a preloading page that does the following :

  • Loads the Images
  • Once the loading is done, redirects the user to the requested page

    <script type="text/javascript">
    <!--//--><![CDATA[//><!--
        var images = new Array()
        var count=0;
        function preload() {
            for (i = 0; i < preload.arguments.length; i++) {
                images[i] = new Image()
                images[i].src = preload.arguments[i]
            }
        if(count==4) {
        window.location = "index.html";
        }
        }
        preload(
            "backgrounds/bg1.jpg",
            "backgrounds/bg2.jpg",
            "backgrounds/bg3.jpg",
            "backgrounds/bg4.jpg"
        )
    //--><!]]>
    

The problem is it redirects directly (I assume that it just starts the download of the image then directly adds one to the counter variable, quickly reaching 4 and not giving the image the time to download.

Any ideas how I can either make it signal me when the images have finished downloading, or only execute the redirect after it has done downloading the images ?

  • 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-18T00:47:07+00:00Added an answer on June 18, 2026 at 12:47 am

    You need to wait for the load event. It’s quite simple:

    function preload(images, timeout, cb) {
      var cbFired = false,
          remaining = images.length,
          timer = null;
    
      function imageDone() {
        remaining--;
    
        if(remaining === 0 && !cbFired) {
          cbFired = true;
          clearTimeout(timer);
          cb();
        }
      }
    
      function timerExpired() {
        if(cbFired)
          return;
    
        cbFired = true;
        cb();
      }
    
      for(var i = 0; i < images.length; i++) {
        var img = new Image();
        img.onload = imageDone;
        img.onerror = imageDone;
        img.src = images[i];
      }
    
      timer = setTimeout(timerExpired, timeout);
    }
    

    You need to check a few things so that users don’t get stuck:

    • You need to wait for both load and error so that the page doesn’t get stuck if an image fails to load.
    • You should set a maximum timeout.
    • Also, in your code, i was a global variable (no var declaration).

    Here’s how to use it:

    var images = [ "backgrounds/bg1.jpg",
        "backgrounds/bg2.jpg",
        "backgrounds/bg3.jpg",
        "backgrounds/bg4.jpg"];
    
    preload(images, 10000 /* 10s */, function () {
      window.location = 'next_page';
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been developing a magazine engine which behaves like a web page. Currently
I have recently been developing a website which relies a large part on the
I have been developing in Node.js recently and have a good idea of what
I have been working with databases recently and before that I was developing standalone
I recently launched a website I have been developing over the past year -
I have recently been faced with a rather odd task, one result being the
I've recently been developing on the django platform and have stumbled upon Django Forms
I've just recently started developing a site for iPhone using jQTouch, and have the
I have recently been developing a newsletter for a client of mine. How ever
I have been developing web applications for some time but have very recently been

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.