You shall have in mind, that JavaScript works asynchronously. So after you define a closure/function for onreadystatechange
the code continues with execution. In the end the lines like
generatedidea += nouns[n1] + " "; generatedidea += verbs[v1] + " a ";
will be executed before you parse the data retrieved by the AJAX call (site note: Have you defined the variables gt
, a1
, ... properly?)
I recommend you, to use jQuery and their API for deferred objects. For example:
var ajaxCall1 = $.get("url1", function (data) { /* do parsing here */ });
var ajaxCall2 = $.get("url2", function (data) { /* do parsing here */ });
$.when(ajaxCall1, ajaxCall2, ...).done(function () {
// every AJAX call is done, do the rest...
});