After learning that JavaScript is single-threaded (as discussed in this question: If Javascript is not multithreaded, is there any reason to implement asynchronous Ajax Queuing?), I started to explore how this concept applies to my developed application. Here is a snippet of the code:
function GetSQLTable() {
var str = $("#<%=fieldDatabaseReferences.ClientID%>")[0].value
var res = str.split(",");
$("#LoadingImage").show();
$("#LoadingImage2").show();
for (var i = 0; i < res.length; i++) {
(function (i, res) {
setTimeout(function () {
GetSQLTable2(i, res.length, res)
}, 0);
})(i, res);
}
}
function GetSQLTable2(i,reslength,res) {
//if (i == 0)
//{
// var start = new Date().getTime();
//}
var div = document.createElement('div');
div.id = "div" + i
document.getElementById('info_div').appendChild(div);
var PossiblesPage = false;
$.ajax({
type: "POST",
url: "PrimaryNominalAjax.aspx/GetSQLTable",
data: '{username: "' + $("#<%=fieldUserName.ClientID%>")[0].value + '", terminalname: "' + $("#<%=fieldTerminalName.ClientID%>")[0].value + '", terminalip: "' + $("#<%=fieldTerminalIP.ClientID%>")[0].value + '", mappingid: "' + res[i] + '", usergroup: "' + $("#<%=fieldUserGroup.ClientID%>")[0].value + '", usn: "' + $("#<%=fieldUSN.ClientID%>")[0].value + '", requester: "' + $("#<%=fieldRequester.ClientID%>")[0].value + '", reason: "' + $("#<%=fieldReason.ClientID%>")[0].value + '", rrd: "' + $("#<%=fieldRRD.ClientID%>")[0].value + '", review: "' + $("#<%=fieldReview.ClientID%>")[0].value + '", possibles: "' + PossiblesPage + '",linkno: "", urn1: "", urn2: ""}',
contentType: "application/json; charset=utf-8",
timeout: 80000000,
dataType: "json",
success: OnSuccess(i, reslength),
error: OnError,
failure: function (response) {
alert('there was an error loading the webpage')
}
});
}
The server side populates the fieldDatabaseReferences. The AJAX requests data from multiple local databases, with the information being displayed on the screen as it becomes available.
Given that the calls to various database servers are asynchronous, could this potentially have a multi-threaded effect?