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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:54:33+00:00 2026-05-20T18:54:33+00:00

I have an async call that, when it completes, I want to set a

  • 0

I have an async call that, when it completes, I want to set a property on the parent.

Take the following class, cat:

var cat = function(){
   this.isLoaded = false;
   this.image = new Image();
   this.image.onload = function(){
     //how can I set the isLoaded variable above from here??
   }
   this.image.src = "...";
}

I want to be able to do:

var myCat = new cat();
if(myCat.isLoaded)
....

I cant seem to figure out how to set the isLoaded property above from within the onload. I can access it by just doing ‘isLoaded’ but I think I am accessing it by value, not reference, and thus cant change it.

This seems like an easy solution, I guess I lack the deeper understanding.

Thanks!

  • 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-20T18:54:34+00:00Added an answer on May 20, 2026 at 6:54 pm

    Inside the onload callback, isLoaded is undefined, and if you set it, it will be window.isLoaded (essentially a global).

    The callback’s this too points to an img element, not its parent function. I made a that variable that points to the outer function’s this. Functions in functions have access to their outer function’s scope in JavaScript.

    var Cat = function(){
       this.isLoaded = false;
       this.image = new Image();
       var that = this;
       this.image.onload = function(){
         that.isLoaded = true;
       }
       this.image.src = "http://www.gravatar.com/avatar/5e28492984e3056904e295c43337655f?s=32&d=identicon&r=PG";
    }
    
    var c = new Cat();
    
    console.log(c.isLoaded); // false
    
    setTimeout(function() { console.log(c.isLoaded) }, 1500); // true
    

    jsFiddle.

    Of course, the setTimeout() is used for this example only. If you are loading an image, you should rely on callbacks, because the only other way to determine if the image has loaded is by polling – not a very good interface.

    You should provide a place to pass in a callback, so you can do…

    var c = new Cat(function() {
       // Image loaded, proceed.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that basically stores files in amazon s3. Here is what
I have a standard asmx webmethod, that when referenced creates the following method in
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a NSURLConnection that is working fine when I allow the load to
In the code below, I have a List object. I want to iterate through
I have 2 functions that needs to be executed one after the other. In
I have a client application that scrapes data into a raw table. This client
I have a recursive call which includes an asyn operation (file copy) .. I
On my website I use a long polling jquery ajax function. This ajax call
I have a project that adds elements to an AutoCad drawing. I noticed that

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.