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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:32:38+00:00 2026-05-26T21:32:38+00:00

I have the following js + jQuery code with some misbehaving var scopes…or at

  • 0

I have the following js + jQuery code with some misbehaving var scopes…or at least a misbehaving coder :-p

var lookup_output="";  //believe this makes it a global scope

function lookupWord(word) {
    var url = 'http://localhost:3000/jianti/' + word + '.json';
    lookup_output = "abc";

    $.getJSON(url, function(data){
            $.each(data, function(i, word_data){
                    lookup_output += "<div> trad: " + word_data.fanti + "<br>";
                    lookup_output += "simp: " + word_data.jianti + "<br>" ;
                    lookup_output += "pinyin: " + word_data.pinyin + "<br>" ;
                    lookup_output += "def: " + word_data.def + "<br>" ;
                    lookup_output += "</div><br>";
                    alert(lookup_output);
            });     
    });

    lookup_output += "123";
    return lookup_output;
}

the problem is that the line

return lookup_output; 

gives back

"abc123"

whereas the line

alert(lookup_output); 

gives

"abc123<div> trad: 交流<br>simp: 交流<br>pinyin: jiāo liú<br>def: exchange/give-and-take/to exchange/to alternate/communication/alternating current (electricity)<br></div><br>"

i want the return line to give out the same as the output…any ideas how? i thought that by attaching it to a global variable it would do it, but seems not.

  • 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-26T21:32:38+00:00Added an answer on May 26, 2026 at 9:32 pm

    You don’t have a scoping problem, you have a problem with the asynchronous nature of the $.getJSON call. $.getJSON is an AJAX call and the “A” in “AJAX” stands for “asynchronous”. When you call $.getJSON, it will return you’ll continue on yo lookup_output += "123", then later, the $.getJSON callback function will get called.

    The usual approach is to add more callbacks:

    function lookupWord(word, callback) {
        var url = 'http://localhost:3000/jianti/' + word + '.json';
    
        $.getJSON(url, function(data){
            var lookup_output = "abc";
            $.each(data, function(i, word_data){
                lookup_output += "<div> trad: " + word_data.fanti + "<br>";
                //...
            });
            lookup_output += "123";
            callback(lookup_output);    
        });
    }
    

    Then, you’d call it like this:

    lookupWord("someword", function(lookup_output) {
        // Do whatever needs to be done with the result.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following code in JavaScript and jQuery: $(<li />) .html('Some HTML') I
I have some jQuery code like this: // Called right away after someone clicks
Hi I have the following jQuery code that splits some content after the second
I have got the following jquery code which appends some html elements to the
I have the following jquery code: $(function() { $('.sliding-buttons').click(slidingContent); }); function slidingContent(e) { var
i have the following jquery code. basically i will have several overlapped divs and
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following JQuery code: // When the document is ready, start firing

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.