I am aware of the asynchronous issues with JavaScript, which is why I have created two functions to encapsulate AJAX calls in reusable functions. These functions work perfectly fine when used with callbacks.
However, I am facing an issue where I need a variable from the getBlocks() AJAX call to be passed to the second AJAX call getAcct(). In the first AJAX call, I have a for loop that triggers the second AJAX call on each iteration to retrieve the account name. I have tried setting a global variable qq, but it always shows as undefined.
How can I resolve this issue?
// Function to get blockchain transactions
function getBlocks(acct, ip, pikachu) {
$.ajax({
url: 'http://'+ ip +'/nxt?=%2Fnxt&requestType=getBlockchainTransactions&account=' + acct + '+&withMessage=true',
dataType: 'json',
success: pikachu
}); // Get account name
}
// Function to get account info
function getAcct(acct, ip, zelda) {
$.ajax({
url: 'http://'+ ip +'/nxt?=%2Fnxt&requestType=getAccount&account=' + acct + '+&withMessage=true',
dataType: 'json',
success: zelda
}); // Get account name
}
getBlocks(nxtacct, nxtip, function(response) {
var xx = [];
xx = response.transactions;
for( i = 0; i < xx.length; i++) {
yy = xx[i].senderRS;
var qq;
getAcct(yy, nxtip, function(response) {
qq = response.name;
});
jQuery(targetb).append("<tr>");
jQuery(targetb).append("<td>" + xx[i].amountNQT +"</td>");
jQuery(targetb).append("<td>" + xx[i].feeNQT + "</td>");
jQuery(targetb).append('<td><a href="https://test.nxtportal.org/accounts/'+ xx[i].sender +'" target="_blank">' + qq + "<em>" + yy + "</em>" + "</a></td>");
jQuery(targetb).append("<td>" + xx[i].recipientRS + "</td>");
jQuery(targetb).append("<td>" + xx[i].message+ "</td>");
jQuery(targetb).append("<td>" + date +"</td>");
jQuery(targetb).append("</tr>");
} //for
});