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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:28:56+00:00 2026-05-15T06:28:56+00:00

I am working on a bit of javascript to plot data on a <canvas

  • 0

I am working on a bit of javascript to plot data on a <canvas>. The data points are marked by one of several different (small) image files. I am attempting to have the plot method wait until all the images are loaded. My best attempt thus far is such:

var icon = {
    left : {
        air : new Image(),
        bone : new Image(),
    },
    right : {
        air: new Image(),
        bone : new Image(),
    },
};

icon.left.air.src   = option.imgPath + 'left.air.png';
icon.right.air.src  = option.imgPath + 'right.air.png';
icon.left.bone.src  = option.imgPath + 'left.bone.png';
icon.right.bone.src = option.imgPath + 'right.bone.png';


function main() {
    Canvas.draw();

    // Make sure our image icons are loaded before we try to plot
    $(icon.left.air).load(function() {
        $(icon.right.air).load(function() {
            $(icon.left.bone).load(function() {
                $(icon.right.bone).load(function() {
                    Data.plot();
                });
            });
        });
    });
}

This works as expected most of the time. On occasion, it will fail and no data will be plotted. Inserting several console.log() statements shows that the script will silently stop working through the series of .load() statements, though code that comes after will be executed.

My questions are as follows: Am I approaching this the right way? Is there a way to attach an event to my icon object that will fire once all of the images inside are loaded?

This is a jquery plugin, so obviously jquery-based solutions are just as acceptable as vanilla javascript.

  • 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-15T06:28:57+00:00Added an answer on May 15, 2026 at 6:28 am

    This should do the trick. It guarantees that Data.plot() will not be called until after all images has loaded and the main function has run.

    ...
    
    function collected(count, fn){
        var loaded = 0;
        return function(){
            if (++loaded === count){
                 fn();
            } 
        }
    }
    
    // four calls + the trigger in 'main'
    var onLoad = collected(5, function(){
        Data.plot();
    });
    
    icon.left.air.load = onLoad;
    icon.right.air.load = onLoad;
    icon.left.bone.load = onLoad;
    icon.right.bone.load = onLoad;
    
    // set the src AFTER load to make this reliable
    icon.left.air.src   = option.imgPath + 'left.air.png';
    icon.right.air.src  = option.imgPath + 'right.air.png';
    icon.left.bone.src  = option.imgPath + 'left.bone.png';
    icon.right.bone.src = option.imgPath + 'right.bone.png';
    
    function main() {
        Canvas.draw();
        onLoad(); // this will be the last (or first, second, third, fourth) call needed to execut Data.plot();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer AFAIK, Cosmos User Kit doesn't support Mono, yet. May 16, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer Answer was provided in RX Forums by Richard Hein. Short… May 16, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer Do you mark your methods with the @Rollback annotation? From… May 16, 2026 at 11:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am looking for a good JavaScript library for parsing XML data. It should
I'm working with Qt ( version 4.6 on Windows XP 32-bit and compiling in
I'm working on a memory pool for a small game engine. The main use
I am working on this small project where I'd like to generate the call
I am trying to use the Bing Maps Ajax Control to plot pins of
I've been working for a few weeks now with the Google Maps API v3,
I've been working in asp.net webforms and I've been making a UserControl that depends
I am working on an ASP.Net web application that must print dynamically created labels
IE6 is always surprising me but this one is REALLY odd. I'm creating a
I'm working on what it looked to be like a very simple feature which

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.