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

  • Home
  • SEARCH
  • 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 787339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:11:07+00:00 2026-05-14T21:11:07+00:00

Sometimes JavaScript doesn’t make sense to me, consider the following code that generates a

  • 0

Sometimes JavaScript doesn’t make sense to me, consider the following code that generates a photo mosaic based on x/y tiles. I’m trying to set a .Done property to true once each Mosaic image has been downloaded, but it’s always false for some reason, what am I doing wrong?

var tileData = [];

function generate()
{
    var image = new Image();
    image.onload = function()
    {
        // Build up the 'tileData' array with tile objects from this Image

        for (var i = 0; i < tileData.length; i++)
        {
            var tile = tileData[i];

            var tileImage = new Image();
            tileImage.onload = function()
            {
                // Do something with this tile Image
                tile.Done = true;
            };
            tileImage.src = tile.ImageUrl;
        }
    };
    image.src = 'Penguins.jpg';

    tryDisplayMosaic();
}

function tryDisplayMosaic()
{
    setTimeout(function()
    {
        for (var i = 0; i < tileData.length; i++)
        {
            var tile = tileData[i];

            if (!tile.Done)
            {
                tryDisplayMosaic();
                return;
            }
        }

        // If we get here then all the tiles have been downloaded
        alert('All images downloaded');
    }, 2000);
}

Now for some reason the .Done property on the tile object is always false, even though it is explicitly being set to true inside tileImage.onload = function(). And I can ensure you that this function DOES get called because I’ve placed an alert() call inside it. Right now it’s just stuck inside an infinite loop calling tryDisplayMosaic() constantly.

Also if I place a loop just before tryDisplayMosaic() is called, and in that loop I set .Done = true, then .Done property is true and alert('All images downloaded'); will get called.

  • 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-05-14T21:11:08+00:00Added an answer on May 14, 2026 at 9:11 pm

    The variable “tile” in the top loop is shared by every one of the “onload” functions you create; the very same single variable, in other words. Try this, to give each one its own variable:

    tileImage.onload = (function(myTile) {
      return function()
            {
                // Do something with this tile Image
                myTile.Done = true;
            };
    })(tile);
    

    Why is this so? Because unlike C or Java, declaring “tile” inside the loop like that does not make it scoped to the statement block of the loop body. The only thing that gives you scope in Javascript is a function body.

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

Sidebar

Related Questions

I have javascript in a webview that only loads sometimes. Other times it doesn't,
sometimes i use debug code to alert something in javascript (for example, matching something
I've noticed that initial comment in JavaScript or CSS files is sometimes started with
It appears that sometimes object.setAttribute(attrib,value) isn't equivalent to object.attrib=value in javascript? I've got the
Sometimes I update my Coffee code in Rails 3.1 and it doesn't detect any
Lets say I pick up a pure Javascript library... one that doesn't rely on
Hi I would like to have an explanation regarding the following code. Here Sometimes
I have some JavaScript that can appear on many different pages. Sometimes those pages
This is my code, which sometimes works and sometimes doesn't. var resolve_ajax_login=function(){ $.ajaxSetup({cache:false });
Sometimes I see @cc_on in JavaScript. What does it mean?

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.