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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:04:57+00:00 2026-05-24T17:04:57+00:00

i got the code here a array like this var pics = [‘1.jpg’,’2.jpg’,’3,jpg’]; loop

  • 0

i got the code here
a array like this var pics = [‘1.jpg’,’2.jpg’,’3,jpg’];

loop throught the array

var j = 0; 
var(var i in pics){ 
$(document.createElement(img)).attr('src',pics[i]).load(function(){
    alert(pics[i]+'is loaded');
    j++;
})}

if(j == pics.length){
alert('fully loaded');}

but all i got is 3,jpg is loaded all the time,
how to solve this? where am i wrong?

  • 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-24T17:04:58+00:00Added an answer on May 24, 2026 at 5:04 pm

    That’s because the .load event fires asynchronously. JavaScript will not wait for all the images to load before proceeding with the script execution, so you need to perform the test each time an image has loaded. I’ve made your code a bit more readable too.

    var len = pics.length;
    var loadCounter = 0;
    for(var i = 0; i < len; i++) {
        $(document.createElement(img)).attr('src', pics[i]).load(function() {
            alert(pics[i] + 'is loaded');
            loadCounter++;
            if(loadCounter === len) {
                alert("all are loaded");   
            }
        });
    }
    

    Side note: traversing an array using for...in will yield nasty results. In particular, it will include all properties of Array, so in short, don’t do it 🙂 See for yourself.

    Another thing, the load event may not fire in some browsers when the image has been cached. To avoid this problem, you can manually fire the .load event on each image which has its complete property set.

    var len = pics.length;
    var loadCounter = 0;
    for(var i = 0; i < len; i++) {
        $(document.createElement(img)).attr('src', pics[i]).one("load", function() {
            alert(pics[i] + 'is loaded');
            loadCounter++;
            if(loadCounter === len) {
                alert("all are loaded");   
            }
        }).each(function() {
            if(this.complete) $(this).trigger("load");
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code here: SqlCommand CodeStatus = new SqlCommand(SQL, DB); DB.Open(); Reader =
Got this line of code here but its not working. private void Button_Click(object sender,
I've got some code here that works great on IPv4 machines, but on our
I have got the following code from here to read an Excel file using
Here is link where i got a code for web-page content fetching. But i
first post here, and probably an easy one. I've got the code from Processing's
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
I've got a program that worked until recently. The offending code is shown here:
Here is what I got so far. Please read the comment in the code.
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:

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.