jQuery async ajax query and returning value problem (scope, closure)

后端 未结 2 2047
悲哀的现实
悲哀的现实 2021-01-28 19:10

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

2条回答
  •  忘了有多久
    2021-01-28 19:19

    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.

提交回复
热议问题