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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:41:16+00:00 2026-05-28T04:41:16+00:00

I have a fiddle located here illustrating my problem, namely that when I load

  • 0

I have a fiddle located here illustrating my problem, namely that when I load images using the Image object, onload gets called but the returned object is still undefined.

Here’s the code in case the fiddle were to disappear into the void:

var ImageManager = function() {
    var _images = [], 
        _loaded = 0;

    var precache = function(imageArray) {
        if (!(imageArray instanceof Array)) {
            return;
        }

        for (var i = 0; i < imageArray.length; i++) {
            if (typeof imageArray[i] !== 'string') {
                continue;
            }

            _images[i] = new Image();
            _images[i].onload = function() { 
                _loaded++;
                document.write('<p>Successfully loaded: ' + imageArray[i] + ' [' + _loaded + '/' + imageArray.length + ']</p>'); 
            }
            _images[i].onerror = function() {
                document.write('<p>Unable to load: ' + imageArray[i] + '</p>'); 
            }
            _images[i].src = imageArray[i];
        }
    };

    var get = function(imagePath) {
        if (!_images.hasOwnProperty(imagePath)) {
            document.writeln('<p>' + imagePath + ' isn\'t precached!</p>');
            return null;
        }

        return _images[imagePath];
    };    

return { get: get, precache: precache }
};

// Test case starts here:

var testImages = [
    'http://jsfiddle.net/img/logo.png',
    'http://jsfiddle.net/img/logo.png',
    'http://jsfiddle.net/img/logo.png'
];

var imageManager = new ImageManager();
imageManager.precache(testImages);

Sample output:

Successfully loaded: undefined [1/3]

Successfully loaded: undefined [2/3]

Successfully loaded: undefined [3/3]

As per usual, it’s likely something obvious I’m overlooking. Aid me, ye all-knowing oracles.

  • 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-28T04:41:16+00:00Added an answer on May 28, 2026 at 4:41 am

    i inside the event handlers are always equal to imageArray.length - 1, because i increases during the loop. Wrap the loop’s body in a closure:

    Also, don’t use document.write, even for debugging purposes. Use console.log(..), alert(..) or at least document.body.innerHTML += ...

    Demo: http://jsfiddle.net/dpDXp/7/

    for (var i = 0; i < imageArray.length; i++) {
        if (typeof imageArray[i] !== 'string') {
            continue;
        }
        (function(i){ //<-- This
    
            _images[i] = new Image();
            _images[i].onload = function() { 
                _loaded++;
                console.log('Successfully loaded: ' + imageArray[i] + ' [' + _loaded + '/' + imageArray.length + ']'); 
            }
            _images[i].onerror = function() {
                console.log('Unable to load: ' + imageArray[i]); 
            }
            _images[i].src = imageArray[i];
        })(i); //<-- This
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fiddle here http://jsfiddle.net/WULsZ/1/ I load jQuery first and the code is
I have a fiddle here that has a big blue div. When i click
I have this fiddle : http://jsfiddle.net/XjeCf/1/ that works like I want and this one
I have a fiddle here - http://jsfiddle.net/hhimanshu/SDr3F/2/ The left pane is already available I
I have created this Fiddle with this code: This is my problem I want
JSfiddle: http://jsfiddle.net/ybZvv/57/ I have a fiddle here where the user can append row and
I have a fiddle here . I set float left & width(20%, 80%) for
I have a fiddle that displays an inset box shadow. I'd like to achieve
I have a Fiddle that contains a div, some text and a button. I'd
I have a fiddle in which i am using knockout.js. I created a custom

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.