I have some question regarding variables and attributions using javascript. I started out my code by doing document.ready() and inside it making two jQuery ajax calls. Later on I decided I need to reuse that code and run those calls again. So I grabbed that code and placed it inside a function. It looks like this.
$(document)
.ready(function() {
var _groupJson;
var _ticketsJson;
_groupJson = groupsAjaxCall(_groupJson);
_ticketsJson = ticketsAjaxCall(_ticketsJson); //rest of code...
Why the _groupJson? It represents an array of data that is filled by the ajax call (it used to work back when the ajax calls were inside document.ready). I need to pass those variables has parameters to other functions, like a print function that shows that data properly formatted in the HTML The function ajaxCall looks a bit like this:
function groupsAjaxCall(groups){
$.ajax({
url: '/api/groups',
dataType: 'json',
success: function(json) {
// get the `groups` array
groups = json;
showinHTML(groups);
},
error: function() {...}
});
return groups; }
The thing is, later on in document.ready
part I do some event handling, and need the content of those _ticketsJson
in other functions. But the _ticketsJson
that the other functions receive is 'undefined'.
So, clearly I'm confusing a lot of concepts as to what does a variable represents in Javascript and how can I do var thisVar = thatVar;
Thank you in advance
EDIT: I don't think this due to the delay of an AJAX call. Inside the something AjaxCall function I can refer to the variable passed as parameter and show that information, I'm even calling there some printing functions that are displaying properly. But outside the function the _groupsJson should be holding the same value... I think... but it's not.