0

I've read a lot of question about this topic. But nobody has my problem. Actualy I have this in my main code:

 // getHtmlInfoWindows has inside an ajax request
 var infowindowString = getHtmlInfoWindow(selectedTrucks[i], true);
 makeInfoWindowEvent(map, infowindow[i], infowindowString, markers[i], "click");

Here the getHtmlInfoWindows() code

 $.ajax({
    url: ajaxUrl,
})
.done(function(data) {
     // some line of code and operation
     return someData;
}

Basically I would to wait until getHtmlInfoWindow() finish and next execute makeInfoWindowEvent() but I don't know how.

I've tried this:

 $.when(infowindowString = getHtmlInfoWindow(selectedTrucks[i], true)).done(function(){}

but doen't work because a have to return the intere ajax response and not the single value "someData" that I want. How can I do?

Thanks guys

Aso Strife
  • 956
  • 10
  • 26
  • `getHtmlInfoWindows()` will never return anything, no matter how long you wait. – JJJ May 26 '16 at 09:47
  • You can make a synchronous call (calling code waits until request is completed) via jQuery ajax method this way: $.ajax({ url: "/api/function", async: false }).done(function() { // handle result ... }); Note 'async' property of parameter object. Although this method is deprecated since jQuery 1.8 and should be avoided. More details: http://api.jquery.com/jquery.ajax/ – Alexey May 26 '16 at 09:56

1 Answers1

1

Pass the code you want to be executed as a callback function:

// getHtmlInfoWindows has inside an ajax request

var callback = function(data){ 
   makeInfoWindowEvent(map, infowindow[i], data, markers[i], "click"); 
}

getHtmlInfoWindow(selectedTrucks[i], true, callback);

and then call your callback when the ajax is done:

$.ajax({
    url: ajaxUrl,
})
.done(function(data) {
     // some line of code and operation
     callback(data);
}
Shadi Shaaban
  • 1,505
  • 1
  • 7
  • 16