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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:55:13+00:00 2026-05-11T06:55:13+00:00

Ok, so that title probably doesn’t explain my question well. Hopefully this makes sense.

  • 0

Ok, so that title probably doesn’t explain my question well. Hopefully this makes sense. This is also my first application with jQuery, so forgive me if I’m doing something dumb.

I have the following function:

function getRandomImages(limit) {     imagesArray = new Array();     $.getJSON('createImageArray.php', {limit: limit}, function(data) {         $.each(data, function(i) {             imagesArray[i] = data[i];  //imagesArray is declared globally.         });     });  } 

The getJSON is correctly grabbing the JSON object. It returns something like this:

{'image0':'images/19.10.FBB9.jpg','image1':'images/8.16.94070.jpg','image2':'images/8.14.47683.jpg','image3':'images/8.15.99404.jpg','image4':'images/8.13.20680.jpg','image5':'images/21.12.9A.jpg','image6':'images/8.17.75303.jpg'} 

I was debugging and am confident that data[i] correctly contains the image path as grabbed from the JSON object. However, after getRandomImages() is called, I look at my global imagesArray and notice that nothing has been changed. I’m guessing it’s creating a copy of the imagesArray instead of grabbing the actual one.

Can someone tell me what I need to do so that my global imagesArray gets updated in the $.each block? Do I need to pass in imagesArray by reference somehow? Sorry, I’m a bit lost.

Thanks for the help.

EDIT: Some background information. I am populating an array of random image locations from the DB. I don’t want to load all the images from the db to an array at once, because there are just too many. So, I have a counter which keeps track of where I am in my image array. Once I’m done with an image, I move the pointer to the next image. If I reach the end, I need to grab more random images. That’s where the above js function gets called; it calls createImageArray.php which grabs x random images from the db and returns an array. I then want to store those image locations in my global imagesArray.

I’m not sure how I would restructure my code to take .getJSON’s asynchronouos nature into account.

  • 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. 2026-05-11T06:55:14+00:00Added an answer on May 11, 2026 at 6:55 am

    What happens here is that the getJSON is called asynchronously. So the function returns immediately and the code inside it is called at some point later at time. It was only then that the imagesArray would be updated.

    You could have the getJSON behave synchronously, but I wouldn’t recommend it. Instead I recommend restructuring your code to take into consideration the asynchronous nature of AJAX requests.

    EDIT: For making a synchronous request use the async option (link). Use the general $.ajax function or set AJAX global options. The reason that I don’t recommend this is, that it locks the browser. If your server fails to respond in a timely fashion, the user will not be able to use his/her browser.

    It isn’t that difficult to restructure your code. Just move your logic for handling image arrays in the callback function. For example instead of:

    function getRandomImages(limit) {     imagesArray = new Array();     $.getJSON('createImageArray.php', {limit: limit}, function(data) {         $.each(data, function(i) {                 imagesArray[i] = data[i];  //imagesArray is declared globally.         });     });     processImages(); } 

    do

    function getRandomImages(limit) {     imagesArray = new Array();     $.getJSON('createImageArray.php', {limit: limit}, function(data) {         $.each(data, function(i) {                 imagesArray[i] = data[i];  //imagesArray is declared globally.         });         processImages();     });  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, that title probably doesn't make much sense, but what I want to know
First of all I'd welcome edits to the title of this question, I couldn't
I think the title for this question is probably wrong, but I'm not sure
Ok, that probably doesn't make a ton of sense... let me illustrate: <div class=csl-entry>
That title probably doesn't sound right, but forgive me, I'm learning Rails for the
That title may need some work. Perhaps after reading this, you may be able
Does that title sound confusing? I thought it might well anyways. When I person
I'm not sure that title is worded very well, but not sure how else
First of all, sorry about that title. I'm not the best at writing those
(I realize that my title is poor. If after reading the question you have

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.