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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:52:19+00:00 2026-05-27T20:52:19+00:00

I want a function to run when specific images are loaded, but I don’t

  • 0

I want a function to run when specific images are loaded, but I don’t know how to wait for both to load before running. I only know how to chain them, like below:

Image1 = new Image();
Image1.src = 'image1-link.jpg';

Image2 = new Image();
Image2.src = 'image2-link.jpg';

Image1.onload = function() {
    Image2.onload = function() { ... }
}

The downside to this is it has to wait till Image1 completely loads before getting the second. I want to try something like this:

Image1 = new Image();
Image1.src = 'image1-link.jpg';

Image2 = new Image();
Image2.src = 'image2-link.jpg';

(Image1 && Image2).onload = function() { ... }

EDIT: Thank you both for the help Paul Grime and ziesemer. Both your answers are very good. I think I have to give the best answer to Paul Grime, although his uses more code, I only need to know the src of the images and the onload function is built in while in ziesemer’s answer I need to know both the src, the count of the images, and have to write multiple onload lines.

Depending on the situation, both have their purpose. Thank you both for the help.

  • 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-27T20:52:20+00:00Added an answer on May 27, 2026 at 8:52 pm

    This fiddle might help. It’s a simple generic ‘loader’.

    http://jsfiddle.net/8baGb/1/

    And the code:

    // loader will 'load' items by calling thingToDo for each item,
    // before calling allDone when all the things to do have been done.
    function loader(items, thingToDo, allDone) {
        if (!items) {
            // nothing to do.
            return;
        }
    
        if ("undefined" === items.length) {
            // convert single item to array.
            items = [items];
        }
    
        var count = items.length;
    
        // this callback counts down the things to do.
        var thingToDoCompleted = function (items, i) {
            count--;
            if (0 == count) {
                allDone(items);
            }
        };
    
        for (var i = 0; i < items.length; i++) {
            // 'do' each thing, and await callback.
            thingToDo(items, i, thingToDoCompleted);
        }
    }
    
    function loadImage(items, i, onComplete) {
        var onLoad = function (e) {
            e.target.removeEventListener("load", onLoad);
    
            // this next line can be removed.
            // only here to prove the image was loaded.
            document.body.appendChild(e.target);
    
            // notify that we're done.
            onComplete(items, i);
        }
        var img = new Image();
        img.addEventListener("load", onLoad, false);
        img.src = items[i];
    }
    
    var items = ['http://bits.wikimedia.org/images/wikimedia-button.png',
                 'http://bits.wikimedia.org/skins-1.18/common/images/poweredby_mediawiki_88x31.png',
                 'http://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/30px-Commons-logo.svg.png',
                 'http://upload.wikimedia.org/wikipedia/commons/3/38/Icons_example.png'];
    
    loader(items, loadImage, function () {
        alert("done");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP: run function when a specific class method is run what I want is
Possible Duplicate: C# Execute function at specific time I want to run certain function
I only want this function to run if .toolbar li does not have the
I want to run function inside web service (.asmx file) $.ajax({ type: POST, contentType:
I have problem with fancybox. I want to write a function that will run
I have a C# application, and I want to run a function which is
I want to run a simple JavaScript function on a click without any redirection.
Want the function to sort the table by HP but if duplicate HPs then
We want to know if it is possible to have a function using jQuery
how to run the async task at specific time? (I want to run it

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.