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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:22:10+00:00 2026-06-13T16:22:10+00:00

Back again asking silly questions… :) I’ve made a few functions, one for storing

  • 0

Back again asking silly questions… 🙂

I’ve made a few functions, one for storing images, one for retrieving and one for checking if images are loaded. Images are stored in an array like this:

tiles = [ ["ID", "Image Object", "Loaded boolean"] ];

However, the order in which Javascript runs my code boggles me. I’ve added several console.logs to see what’s going on in the code and this is what I’m getting:

    START
    Storing http://funxion.wippiespace.com/js/mario/question.gif...  
    Storing http://funxion.wippiespace.com/js/mario/wall.gif...  
    Storing http://funxion.wippiespace.com/js/mario/floor.gif...  
    Map.draw initiated  
    Checking if all images are loaded...  
    Amount of tile images = 3  
    Checking [0]  
    [0]: q,[object HTMLImageElement],false is not loaded  
    Images not loaded!  
    http://funxion.wippiespace.com/js/mario/question.gif stored!  
    http://funxion.wippiespace.com/js/mario/wall.gif stored!  
    http://funxion.wippiespace.com/js/mario/floor.gif stored!  

I dont understand why the loaded=true is the last thing in there. Also, I’ve tried putting a setTimeout(this.draw, 1000); in the Map.prototype.draw so there would be a 1sec delay before checking allImgLoaded again, that doesn’t work. Without the loading check drawing works fine. Also, Start() is called in .

The code:

    var canvas = document.getElementById("surface");
    var ctx = canvas.getContext("2d");

    function Map()
    {
        this.tiles = [];
    }
    Map.prototype.draw = function(tileid)
    {
        console.log("Map.draw initiated");
        if (!allImgLoaded(this.tiles))
        {
            console.log("Images not loaded!");
        }
        else
        {
            console.log("All good, proceeding.");
            var img = this.getImg(tileid);
            ctx.drawImage(img, 100, 100);
        }
    }
    Map.prototype.storeImg = function(identifier, imgSrc)
    {
        var nextIndex = this.tiles.length;
        var tile = [identifier, new Image(), false];
        tile[1].src = imgSrc;
        console.log("Storing " + imgSrc + "...");
        tile[1].onload = function()
        {
            tile[2] = true;
            console.log(this.src + " stored!");
        }
        this.tiles[nextIndex] = tile;

    }
    Map.prototype.getImg = function(identifier)
    {
        for (var i in this.tiles)
        {
            console.log("Checking index " + i + " for " + identifier + "...");
            if (this.tiles[i][0] === identifier)
            {
                console.log("Found " + this.tiles[i][1] + "! Returning it now.");
                return this.tiles[i][1];
            }
        }
    }

    function allImgLoaded(array)
    {
        console.log("Checking if all images are loaded...");
        console.log("Amount of tile images = " + array.length);
        for (var i in array)
        {
            console.log("Checking ["+i+"]");
            if(array[i][2] === false)
            {
                console.log("["+i+"]: " + array[i] + " is not loaded");
                return false;
            }
        }
        console.log("All loaded!");
        return true;
    }
    function Start()
    {
        console.log("START");
        var mappi = new Map();
        mappi.storeImg("q", "http://funxion.wippiespace.com/js/mario/question.gif");
        mappi.storeImg("w", "http://funxion.wippiespace.com/js/mario/wall.gif");
        mappi.storeImg("f", "http://funxion.wippiespace.com/js/mario/floor.gif");
        mappi.draw("w");
    }
  • 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-13T16:22:11+00:00Added an answer on June 13, 2026 at 4:22 pm

    You don’t seem to understand that the loading of images is asynchronous. It happens in the background while your other javascript continues to run. You start the operation, your other code continues to run and some time later the images finish loading the onload callback function gets called.

    If you want to run an operation AFTER an image is loaded, you need to start that operation from the onload handler for that image. Right now your code starts the loading of all the images and then you immediately call mappi.draw() before the images are done loading. More specifically .storeImg() just starts the loading of an image. The actual loading of the image is not done until some time later (as you can see from your console.log() trace.

    If you want to preload a bunch of images and then trigger an operation when all the images are loading, you can use code like is found in this answer: Image preloader javascript that supports events which will start the loading of a bunch of images and then call a callback when they are all loaded. You could then call your .draw() method from that callback. Using callbacks like this is part of asynchronous programming in javascript and is required anytime something is being loaded over the network (an image, an ajax call, etc…).

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

Sidebar

Related Questions

I am back with a problem again (please cooperate with my silly questions, i
Back again with a javascript question within sharepoint. I would like to hide a
back again with some more SQLAlchemy shenanigans. Let me step through this. My table
I am trying to convert a Date to String and then back again to
I'm back. Again. :3 Right now, I'm working on my RPG project (just for
When the user switches to another program and then back again., the original program's
Hey guys back again with a whole new project for myself. In my earlier
im asking again :), i don't know how make this. My English is not
I remember asking this question a while back but i can't seem to find
EDIT AGAIN : I don't want to create another question, so asking here. I

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.