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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:03:30+00:00 2026-05-15T07:03:30+00:00

Code not working because of async query and variable scope problem. I can’t understand

  • 0

Code not working because of async query and variable scope problem. I can’t understand how to solve this. Change to $.ajax method with async:false – not an option. I know about closures, but how I can implement it here – don’t know. I’ve seen all topics here about closures in js and jQuery async problems – but still nothing. Help, please.
Here is the code:

var map = null;
var marker;
var cluster = null;

function refreshMap() 
{
    var markers = [];  
    var markerImage = new google.maps.MarkerImage('/images/image-1_32_t.png', new google.maps.Size(32, 32));

    $.get('/get_users.php',{},function(data){
        if(data.status == 'error')
            return false;

        var users = data.users; // here users.length = 1 - this is ok;  
        for(var i in users)
        {
            //here I have every values from users - ok
            var latLng = new google.maps.LatLng(users[i].lat, users[i].lng);
            var mark = new google.maps.Marker({
                position: latLng,
                icon: markerImage
         });

             markers.push(mark);
             alert(markers.length); // length 1
        }

    },'json');

    alert(markers.length); // length 0  
    //if I have alert() above - I get result

    cluster = new MarkerClusterer(map, markers, 
    {
        maxZoom: null,
        gridSize: null
    });
}

Thanks.

  • 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-15T07:03:31+00:00Added an answer on May 15, 2026 at 7:03 am

    Just move this code:

    cluster = new MarkerClusterer(map, markers, 
    {
        maxZoom: null,
        gridSize: null
    });
    

    Into the callback function (where your first alert is)

    The problem is that with an async request the code will continue to execute even though the request has not completed. So your markers variable isn’t set properly until your anonymous callback function is executed.

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

Sidebar

Related Questions

runat=server /> But this code is not working How can I add multiple controls
How to override TIniFile.Create constructor ? This code is not working because Create is
My code looks fine but it is not working as it should because update
can anybody tell me why is the following code not working? <script type=text/javascript src=../../Scripts/jquery.js></script>
The following code is not working because default seems to be a JavaScript keyword.
Why is this code not working ? <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
This code is not working for me on Delphi XE: http://delphi.about.com/cs/adptips2000/a/bltip0800_5.htm procedure TForm1.FormDeactivate(Sender: TObject)
current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)
i am using python 2.5.2 . The following code not working. def findValue(self, text,

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.