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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:06:26+00:00 2026-05-30T09:06:26+00:00

I work with JSON and I want to count the number of elements in

  • 0

I work with JSON and I want to count the number of elements in the response.

    $.getJSON("/api/getEvents", function(data) {
        $.each(data, function(key, event) {
            var count = 10;
            $.getJSON("/api/getUsers", function(data) {
                $.each(data, function(key, event) {
                    alert("Value: " + count);
                    count++;
                });
            });
            alert("Count: " + count);
        });
    });

As a result, I get:

Value: 10
Value: 11
Value: 12
...
Count: 10

Why count = 10?

  • 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-30T09:06:28+00:00Added an answer on May 30, 2026 at 9:06 am

    It’s because ajax requests are asynchronous. $.getJSON just initiates a request, but javascript execution immediately continues. You can see the count if you move the alert inside the ajax callback:

    $.getJSON("/api/getEvents", function(data) {
        $.each(data, function(key, event) {
            var count = 10;
            $.getJSON("/api/getUsers", function(data) {
                $.each(data, function(key, event) {
                    alert("Value: " + count);
                    count++;
                });
    
                // I moved this here:
                alert("Count: " + count);
            });
            // It used to be here.
        });
    });
    

    So after you set var count = 10, the javascript parser then runs $.getJSON, but then immediately goes on to the next line, which in your code example alerted the “Count: 10”. Then, whenever the request finishes, it runs the callback code that increments the count and alerts the Value lines.

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

Sidebar

Related Questions

I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
Does anyone know why jquery events don't work with json data parsed through Popcorn.js
My json data could have any number of objects from a database. This all
I want to parse a JSON string: MyJsonString: { status: ok, count: 2, count_total:
I'm writing a PHP script to work with some JSON data. Below is an
I have no problems getting the Json to work and parse the json return.
I'm studying javascript and I'm trying to work with some json files. I'm a
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
I'm work with GWT and parsing a JSON result from an ASP.NET webservice method

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.