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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:58:30+00:00 2026-05-31T08:58:30+00:00

I wanted to call a function when all required images are loaded. The number

  • 0

I wanted to call a function when all required images are loaded. The number of images is known in advance, so I tried attaching a function call to the onload event of each image and count the number of times it was called.

<html>

<head>
    <script>

    var tractor;

    function Tractor()
    {
        this.init_graphics();
    }

    Tractor.prototype.init_graphics = function()
    {
        this.gr_max = 3;
        this.load_count = 0;

        this.loading_complete(); // #1 test call, works OK

        this.img1 = new Image();
        this.img1.onload = this.loading_complete; // #2 gets called, but gr_max = undefined, load_count = NaN
        this.img1.src = "http://dl.dropbox.com/u/217824/tmp/rearwheel.gif"; //just a test image
    }

    Tractor.prototype.loading_complete = function()
    {
        this.load_count += 1;
        alert("this.loading_complete, load_count = " + this.load_count + ", gr_max = " + this.gr_max);
        if(this.load_count >= this.gr_max) {this.proceed();}
    };

    function start()
    {
        tractor = new Tractor();
    }
    </script>
</head>

<body onload="start();">
</body>

</html>

When it’s just called from another function of the object (see #1), it works just as I expected. When, however, it’s called from onload event (see #2), the variables become “undefined” or “NaN” or something. What’s happening? What am I doing wrong? How do I make it work?

I don’t remember ever creating my own objects in Javascript before, so I certainly deeply apologize for this “what’s wrong with my code” kind of question. I used this article as a reference, section 1.2, mainly.

Just in case, I put the same code on http://jsfiddle.net/ffJLn/

  • 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-31T08:58:31+00:00Added an answer on May 31, 2026 at 8:58 am

    bind the context to the callback:

    this.img1.onload = this.loading_complete.bind(this);
    

    See: http://jsfiddle.net/ffJLn/1/ (same as yours but with this addition)

    Here’s an explanation of how bind works in detail: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

    The basic idea is that it makes this in the bound function equal to whatever you pass as the parameter to bind.

    Another option is to create a closure:

    var self = this;
    this.img1.onload = function() { self.loading_complete() };
    

    Closures are functions that keep references to their context (in fact, all functions in javascript work this way). So here you are creating an anonymous function that keeps a reference to self. So this is another way to maintain context and for loading_complete to have the right this.

    See: http://jsfiddle.net/ffJLn/2/ (same as yours but with the second possibility)

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

Sidebar

Related Questions

I wanted to call Test1() Method Within WaitAndCallFunc() Function. Code: typedef void (*func)(); void
Update: What I really wanted all along were greenlets . Note: This question mutated
All, I wanted to know how I could pass a $variable from view, for
I am writing a program that will call a function from an external library,
I would want to use time to enable event's callback function in Tkinter. How
I apologize in advance for the long-winded question but I wanted to make sure
I am invoking a javascript function for drilling down the chart. I tried using
I wanted to export a simple function from a base class that I use
Basically when I click a button I want to call a function and run
Wanted to convert <br/> <br/> <br/> <br/> <br/> into <br/>

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.