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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:53:05+00:00 2026-05-22T18:53:05+00:00

It’s very clear I don’t understand how functions are scoped in Javascript. Here’s the

  • 0

It’s very clear I don’t understand how functions are scoped in Javascript. Here’s the latest example:

function riseData() { 
    var jsonResult;
    $.ajax({
        success: function(data, textStatus, jqXHR){
            jsonResult = jqXHR.responseText;
            alert("Inside: " + jsonResult);
        },          
        error: function (jqXHR, textStatus, errorThrown) {  
            $('#errLog').append('<br>Status: ' + qXHR.statusText);                  
        }
    });
    return jsonResult;
}

$(document).ready(function(){
    var intervalID = setInterval('UTCclock()',100); 
    alert("Outside: " + riseData());                            
});

When I execute this, the “Inside” alert functions properly, but the “Outside” alert displays “undefined”, even though riseData() is obviously defined only a few lines earlier. There is a $.ajaxSetup earlier in the code which defines the parameters for the ajax call. The ajax call successfully returns the requested data in the “Inside” alert.

I just haven’t the slightest clue how to make the necessary data (jqXHR.responseText) available to other parts of the script.

Can anyone point me at a “Javascript Scoping for Dummies” how-to that addresses this issue?

  • 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-22T18:53:05+00:00Added an answer on May 22, 2026 at 6:53 pm

    Your example is scoped just fine. Your problem is you don’t understand the implications of how JavaScript is mainly asynchronous.

    Let me go through your example to show you.

    1. Your document ready handler runs.
    2. It calls riseData.
    3. riseData declares a variable, jsonResult.
    4. riseData calls $.ajax, which schedules an AJAX request to be done, but the response will happen later.
    5. Since $.ajax is usually non-blocking/asynchronous, riseData continues and returns jsonResult. Since jsonResult has not been assigned yet because the AJAX request has not completed, the default value of undefined is returned.
    6. In the document ready handler, you end up with alert("Outside: "+undefined);.

    An event loop to a few milliseconds later, this happens:

    1. The AJAX request is completed. jQuery forwards this on to your callback.
    2. jsonResult is set, but riseData has already returned.
    3. The alert inside riseData alerts with the new data, after the document ready handler has already alerted undefined.

    There are two solutions to this problem. The first solution is simple, but often results in a worse user experience. This solution is to add the async: false option to $.ajax. This makes $.ajax block and freeze the browser.

    The second solution is to adopt an asynchronous style for almost everything. That means making riseData accept a callback and calling it inside the AJAX response handler. Here’s your code transformed with this method:

    function riseData(callback) { 
        $.ajax({
            success: function(data, textStatus, jqXHR){
                var jsonResult = jqXHR.responseText;
                alert("Inside: " + jsonResult);
                callback(jsonResult);
            },          
            error: function (jqXHR, textStatus, errorThrown) {  
                $('#errLog').append('<br>Status: ' + qXHR.statusText);                  
            }
        });
    }
    
    $(document).ready(function(){
        //var intervalID = setInterval('UTCclock()',100); 
        // Unrelated, but it's better to pass a
        // function into setInterval than a string.
        var intervalID = setInterval(UTCclock, 100);
        riseData(function(jsonResult) {
            alert("Outside: " + jsonResult);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.