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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:16:10+00:00 2026-06-09T11:16:10+00:00

function updateRoomsList() { //empty the rooms list $(#rooms).empty(); //fetch the non-joined rooms, id and

  • 0
function updateRoomsList() {
    //empty the rooms list
    $("#rooms").empty();
    //fetch the non-joined rooms, id and name
    $.get('JoinPart', {
        goal: 5,
        userName: $("#userName").html()
    }, function (responseText) {
        var i = 0;
        id = "";
        while (i < responseText.length) {
            if (responseText.charAt(i) == '.') {
                //Now we got the full Id of a room, lets add it
                $.get('JoinPart', {
                    goal: 6,
                    roomId: id
                }, function (responseText) {
                    roomName = responseText;
                    $("#rooms").append('<div id="room' + id + '" class="listItem"><span title="Join Room" class="joinButton">+</span><div class="listItemContent">' + roomName + '</div></div>');
                });
                id = "";
            } else {
                id = id + responseText.charAt(i);
            }
            i++;
        }
    });
}

In this Function there’s a variable id, if I alert(id); after

if(responseText.charAt(i)=='.')

I get the proper value of the id calculated in the else , but when I do alert(id); inside the $.get the id is empty "" , means in the append function, the id has no value, how can I get this id to have the value outside of the $.get function ?

  • 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-09T11:16:15+00:00Added an answer on June 9, 2026 at 11:16 am

    That’s because id is scoped in the outer closure, so your inner callback gets a reference to the last value it’s set at the end of the while loop. If you need a private copy of the id variable inside your second $.get() callback, you need to write

    $.get('JoinPart', {goal :6, roomId :id }, (function (id) {
      return function (responseText) {
        roomName = responseText;
        $("#rooms").append('<div>' + id + '</div>');
      }
    })(id));
    

    When you do this, you create a new scope with a local variable id which is given the exact value id had at the current iteration, and not the local variable id itself, which at the end of the loop (or really just at the next iteration) could be anything.

    The construct (function(arg){})(arg) is called an immediately invoked function expression. Please note that this function returns a new function object, the one required by $.get()

    A closure is a function defined inside another one. A closure, by definition, can access all variables local to the outer function. When you supply a callback for later execution to $.get(), you are in fact using a closure: when the callback will be executed (possibly after several seconds) the JS engine will make id still accessible, but its value obviously depends on the rest of the code.

    If you need to store the current snapshot of a variable for later reading, you need to create a new closure (ie a new scope): we use the IIFE for this purpose. The IIFE is executed right before the actual HTTP request is fired (ie before the $.get() is even called), and it’s the nearest scope for the callback, so it will be used to resolve the id reference when the callback will be executed.

    You can test if you understand the concept by figuring out what the following snippet will print

    function inner() {
      console.log(i);          
    }
    
    for (var i = 0; i < 3; i++) {
      inner();
      setTimeout(inner, 1000);
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function get() { $.post( 'postchatfame.php', { comment: postchatfamemsg.comment.value, userid: postchatfamemsg.userid.value }, function(output) { $('#walls').html(output).show();
function Apple(){ this.name=apple; } function Orange(){ this.name=orange; this.apple = new Apple(); this.apple.onCalled=function(){ alert(this.name); }
function Person() {} Person.prototype.population = 0; Person.prototype.constructor = function(name) { this.name = name; console.log(Name:
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = createCustomURL(WinNetwork.UserName); document.getElementById(psyncLink).src =
function getUser() { FB.api('/me', function(response) { console.log('Response is '+response); alert('Your name is ' +
function controllaFoto(target, username) { var _URL = window.URL || window.webkitURL; var img = new
function logsig() { var username = $(#username).val(); var password = $(#password).val(); var dataString =
function removeNewsItem(id,idStamp,obj){ $('#whiteCover')show(); $('#contentOfBox').html= ('Are you sure you want ot completely remove this news
function stationMenu($scope){ $.ajax({ url: /users/station_names_ajax, type: POST, success: function(data){ $scope.phones = [ {name: Nexus
function updateOption(selection, option) { jQuery('body,html').animate({ scrollTop: jQuery(#msg).offset().top }, 800); jQuery('.'+selection).each(function() { jQuery(img.+selection).removeClass(optionSelected); }); jQuery.ajax({

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.