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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:39+00:00 2026-05-28T07:03:39+00:00

I checked other questions, but they all had information how to make a callback

  • 0

I checked other questions, but they all had information how to make a callback when one specific image has loaded:

var img = new Image();
img.src = "images/img.png";
if (!img.complete) {
    img.onload = function() {
        // code to be executed when the image loads
    }
}

Or a simple check to see if all images are loaded with an ‘if’ statement. Also, $(window).load (or onLoad or whatever) doesn’t work.

In my case I’m loading two images:

var img1 = new Image();
var img2 = new Image();
img1.src = 'images/img1.png';
img2.src = 'images/img2.png';

How can I define a callback similar to the one in the first example, but it gets executed when BOTH images have finished loading?

Thank you for your time.

  • 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-28T07:03:40+00:00Added an answer on May 28, 2026 at 7:03 am

    Here’s a function that will create several images and call a callback when they are all loaded:

    function createImages(srcs, fn) {
       var imgs = [], img;
       var remaining = srcs.length;
       for (var i = 0; i < srcs.length; i++) {
           img = new Image();
           imgs.push(img);
           img.onload = function() {
               --remaining;
               if (remaining == 0) {
                   fn(srcs);
               }
           };
           img.src = srcs[i];
       }
       return(imgs);
    }
    
    var imgs = createImages(['images/img1.png', 'images/img2.png'], myCallback);
    

    P.S. whenever working with .onload for images, you must set the .onload handler before setting the .src value because the onload handler might fire immediately when setting the .src value if the image is in the cache. If you haven’t set the onload handler first, then it may never fire because by the time you set the handler, the image is already loaded. This happens in some browsers. Just always set .onload before .src if you need the onload event.

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

Sidebar

Related Questions

required( dependency-expression ) They give an example of required: #other:checked but I want my
I checked all the topics, but i simply don't know why my script does
There are some other questions where people have problems with timestamp being all zeros.
My error is CS0143, The type 'Microsoft.Kinect.KinectSensor' has no constructors defined I checked other
I hate asking questions like this - they're so undefined... and undefinable, but here
I've checked most of the question that was display regarding my title but they
I've read the lame documentation, and checked other answers. I'd like my Android app
I went in and checked my Transaction log the other day and it was
I have a piece of mature geospatial software that has recently had areas rewritten
Our team at work has enthusiastically adopted a rebase workflow, but we might have

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.