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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:40:15+00:00 2026-05-26T08:40:15+00:00

I am trying to load a series of images inside a div using for

  • 0

I am trying to load a series of images inside a div using for loop and when the file does not exist, the for loop breaks. This code doesn’t seem to work.

I’ve tested many others but no success. Any tips?

HTML:

<div id="project_img">
</div>

JavaScript:

function load_projects()
  var project_array = new Array();
  var project_name;

  for(var i = 0; i < 7; i++) {
    project_array.push(new Image());

    project_name = "project_" + i;

    project_array[i].src = "images/" + project_name + ".png";
    project_array[i].id = project_name;

    $("#project_img").append(project_array[i]);

    $("#" + project_name).error(function() {
      alert(i);
      i = 7;
    })
  }
}

$(document).ready(function() {
  load_projects();
});
  • 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-26T08:40:16+00:00Added an answer on May 26, 2026 at 8:40 am

    The problem is, that the error-callbacks will be called after the for loop has finished (runs from 0 till 7).

    If you want to start loading next image if there is no error loading the current image, you have to change your strategy: you need a success callback, which starts loading the next one. jQuerys load method can be used to set a callback if an image has been successfully loaded:

    $("...").load(function() { ... });
    

    If current image has been loaded and not all images has been displayed, start loading next one:

    $("#" + project_name).load(function() {
        if (i < 7) {
            load_projects(i + 1);
        }
    });
    

    So the for loop isn’t needed anymore. The complete code is:

    var project_array = new Array();
    
    function load_projects(i) {
        project_array.push(new Image());
        project_array[i].id = "project_" + i;
        project_array[i].src = "images/" + project_array[i].id + ".png";
    
        $("#project_img").append(project_array[i]);
    
        $("#" + project_array[i].id).load(function() {
            if (i < 7) {
                load_projects(i + 1);
            }
        });
    }
    
    $(document).ready(function() {
        load_projects(0);
    });
    

    Not neccessary but if you want you can add the error callback again:

        ...
        $("#" + project_array[i].id).error(function() {
            alert('error at ' + i);
        });
        ...
    

    Also see my jsfiddle. (There I have added one existing image.)

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

Sidebar

Related Questions

I trying to load and save setting with this code but when I closing
I am trying to load a series of cities/countries in a textbox to display
i m trying load UIImages from server asynchronously in UITableViewCell. My code worked fine
Trying to load content into a div, then on click load the previous content
Trying to load a small .txt file into mysql but get all my data
trying to load in a bunch of images into a list from a directory...my
While trying to load a Bitmap onto a SWFLoader the Event.COMPLETE event is not
I'm having an issue trying to load a series of sql files through our
I'm trying to load some data using sql loader. Here is the top of
I'm using firebird database and it does not seem to have ROWNUM or ROW_NUMBER()

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.