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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:17:25+00:00 2026-06-12T01:17:25+00:00

I have a code that depends on an array of large images being loaded

  • 0

I have a code that depends on an array of large images being loaded first, so I’m trying to do something like this:

var loading = 0;
    var i = 0;
    var imgs = [];
    var background = [
        'bg1-1', 'bg1-2', 'bg1-3', 'bg1-4',
        'bg2-1', 'bg2-2', 'bg2-3', 'bg2-4',
        'bg3-1', 'bg3-2', 'bg3-3', 'bg3-4',
        'bg4-1', 'bg4-2', 'bg4-3', 'bg4-4'
    ];

    for (i = 0; i < background.length; i++) {
        imgs[i] = document.getElementById(background[i]);
        imgs[i].onload = function() {
            loading++;
        }
    }

    while (loading < 16) {  }

    alert('images loaded!'); //I need to ensure images are fully loaded here.

So, it just hangs and never pulls out of the while loop.

  • 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-12T01:17:26+00:00Added an answer on June 12, 2026 at 1:17 am

    Put the condition and the alert in the callback.

    var loading = 0;
    
    for (i = 0; i < background.length; i++) {
        imgs[i] = document.getElementById(background[i]);
        imgs[i].onload = function() {
            loading++;
            if (loading >= background.length)
                alert("all done");
        }
    }
    

    And really it would be better to reuse that function.

    var loading = 0;
    
    function imgHandler() {
        loading++;
        if (loading >= background.length)
            alert("all done");
    }
    
    for (i = 0; i < background.length; i++) {
        imgs[i] = document.getElementById(background[i]);
        imgs[i].onload = imgHandler;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: obj.foo(); // obj might, or might not
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have this html code: <tr> <td><label><input type=text name=id class=DEPENDS ON info BEING student
I'm drawing an image to a canvas element. I then have code that depends
I have code that looks more or less like the code below but it
I have code that reloads images via HTTP from the main thread and displays
I have some code which depends on Greenlets, and need to remove this dependency.
I have this code that should nGen my main application EXE: using System; using
I have code that depends a relatively small MS Jet (created in Access) database.

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.