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 jquery script. see fiddle here , that loads some scripts depending
I have a Website project that I'm trying to debug using Fiddler. Fiddler doesn't
I have a fiddle here . I set float left & width(20%, 80%) for
I have created fiddle here: http://jsfiddle.net/ozzy/WEGMh/ How it should look is here: Issue is
I have a fiddle here where a black box moves slowly across the screen.
Here's a fiddle I have this code where I tried to make a button
I have a fiddle here: http://jsfiddle.net/maniator/SZpkF/1/ The green box is supposed to move with
I have this fiddle here: http://jsfiddle.net/maniator/MUa7P/ In this example a user picks a certain
I have this fiddle at: http://jsfiddle.net/radi8/EwQUW/33/ Notice that the initial table is defined is:
I have a fiddle here is my JS: $(document).ready(function() { window.qNum = 0; window.$oClone

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.