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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:24:27+00:00 2026-05-25T23:24:27+00:00

So I am loading multiple images onto multiple canvases basically (one image per canvas/ctx

  • 0

So I am loading multiple images onto multiple canvases basically (one image per canvas/ctx – its a slideshow). I want to make sure that each image is loaded before it attempts to draw the image onto the canvas.

Here is the code…

  • In Example 1 i’m using 3 onload events (one for each image)
  • In Example 2 i’m using one onload event, but it is the last image that gets called in the for loop

Question: Can i use Example 2 and be confident to assume that if the last image is loaded, then the images before must be loaded as well?

Also—- I attempted this already, but can it all be done inside a for loop? I wasn’t able to get it to work. See Example 3

Example 1 3 onload events

var img=[0,'slide1','slide2','slide3'];   

for (var i=1;i<=3;i++) {
        img[i] = new Image();
        img[i].src = '/images/slide' + i + '.png'
    }

    img[1].onload = function() { // checks to make sure img1 is loaded
        ctx[1].drawImage(img[1], 0, 0);
    };

    img[2].onload = function() { // checks to make sure img2 is loaded
        ctx[2].drawImage(img[2], 0, 0);
    };

    img[3].onload = function() { // checks to make sure img3 is loaded
        ctx[3].drawImage(img[3], 0, 0);
    };

Example 2 only the last onload event

var img=[0,'slide1','slide2','slide3'];   

for (var i=1;i<=3;i++) {
        img[i] = new Image();
        img[i].src = '/images/slide' + i + '.png'
    }

    img[3].onload = function() { // checks to make sure the last image is loaded
        ctx[1].drawImage(img[1], 0, 0);
        ctx[2].drawImage(img[2], 0, 0);
        ctx[3].drawImage(img[3], 0, 0);
    };

Example 3 one onload in the for loop to do all the onload events

var img=[0,'slide1','slide2','slide3'];   

for (var i=1;i<=3;i++) {
        img[i] = new Image();
        img[i].src = '/images/slide' + i + '.png'

        img[i].onload = function() { // checks to make sure all images are loaded
            ctx[i].drawImage(img[i], 0, 0);
        };

    }

I assume I can’t do example 3 because an image probably won’t be loaded by the time the for loop runs the second time and rewrites the onload event for the next image thus erasing the onload event for the prior image. Correct?

  • 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-25T23:24:28+00:00Added an answer on May 25, 2026 at 11:24 pm

    You’ll have scoping issues with the last for-loop because of closures. In order for something like this to work, you’ll want to break out of the closure by encapsulating your functional assignment in it’s own function. This article explains it well.

    Ideally, your last for-loop would look something like this:

    var images = [0,'slide1.png', 'slide2.png', 'slide3.png'];
    
    // Assign onload handler to each image in array
    for ( var i = 0; i <= images.length; i++ ){
    
       var img    = new Image();
       img.onload = (function(value){
           return function(){
               ctx[value].drawImage(images[value], 0, 0);
           }
       })(i);
    
       // IMPORTANT - Assign src last for IE
       img.src    = 'images/'+images[i];
    }
    

    Also, keep in mind that you’ll need to assign the img.src attribute AFTER the onload handler has been defined for some flavors of IE. (this little tidbit cost me an hour of troubleshooting last week.)

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

Sidebar

Related Questions

I am loading multiple images into a picturebox one at a time. Each time
My problem is, I have multiple images and want to load like pre-loader, but
I am loading multiple images into a class instance and I would like to
What is the best way to design a SSIS package? I'm loading multiple dimensions
Loading images into Flex (size < 100kb) causes IE7 memory increase by a megabyte
Some loading routines in my program takes to long to complete. I want a
After loading a PHP template (using jQuery's load function), this simple script won't make
I have a Movieclip with about five FLV movies embedded on its timeline, one
I started with this question: Multiple Javascript gadgets per page . After reading up
I know how to specify multiple background images using CSS3 and modify how they

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.