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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:26:57+00:00 2026-06-12T19:26:57+00:00

meclass.prototype.switch = function() { var items = []; $.getJSON(‘http://localhost/jsoner.php’, function(data) { $.each(data, function(key, val)

  • 0
meclass.prototype.switch = function() {
var items = [];
$.getJSON('http://localhost/jsoner.php', function(data) {
    $.each(data, function(key, val) { 
        items.push(val);
        alert(items[0]); //this works

    });
});
alert(items[0]); //this does not
}

I have been tinkering with this for awhile and not really getting it. I am having this problem in all of my jquery functions so it is something basic that I just haven’t learnt- and haven’t had luck finding answers on.

  • 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-06-12T19:27:00+00:00Added an answer on June 12, 2026 at 7:27 pm

    The getJSON method is asynchronous. Execution will continue with the following statement straight away. The callback function will be executed some time later, whenever the server has responded to the request.

    For that reason, any code that relies upon the result of the async request will need to be moved inside the callback function.

    This is effectively what happens:

    var items = []; //Declare `items`, empty array
    //Make AJAX request
    alert(items[0]); //It's still an empty array
    //Wait some arbitrary amount of time...
    
    //AJAX request complete, run the callback function
    alert(items[0]); //Inside callback, items now contains elements
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function MyClass(projectName) { this.projectName = projectName; } MyClass.prototype.createHttpRequestObject = function() { /* do something
When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000);
Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ //
What I need is something like: var MyEventPointer = $('body').on('MyCustomEvent','.myClass',function(e){ // do something });
I wanted to add a constant to the prototype of a function constructor (class)
function myClass() { this.nums = [1,2,3]; this.divisor = 2; } myClass.prototype.divideNumsByDivisor = function(){ return
e.g.: function myclass (htmlelement) { this.element = htmlelement; } myclass.prototype.hoverfunc = function() { alert
How can I acces my object variables within a $.each function? function MyClass() {
In JavaScript, I simulate classes like this: function MyClass() {} // Constructor MyClass.prototype.myField =
I have a class like this : var MyClass = function(){ this.field = 'field';

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.