gpt4 book ai didi

javascript - AJAX 以多线程方式运行

转载 作者:行者123 更新时间:2023-12-03 11:12:33 25 4
gpt4 key购买 nike

我知道 JavaScript 是单线程的(如本问题所述: If Javascript is not multithreaded, is there any reason to implement asynchronous Ajax Queuing? ),但是我试图了解这如何应用于我开发的应用程序。请看下面的代码:

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')
}
});
}

fieldDatabaseReferences 在服务器端填充。 AJAX 连接到多个本地数据库(最多 30 个),并在准备就绪时将信息显示在屏幕上。

对各个数据库服务器的调用是异步的。这肯定具有多线程效果吗?

最佳答案

JavaScript 是单线程的。当异步事件发生时,它们会被插入队列等待执行,直到线程空闲。考虑以下示例:

var run = true;
var brk = Date.now() + 5000; // five seconds from now
setTimeout(function(){
run = false; // set the run variable to false _asynchronously_
}, 1000); // after one second
while(run && Date.now() < brk); // loop while both conditions are true
console.log("run:", run); // logs run: true (which was the initial value)

你认为循环什么时候会终止?一秒?不,它会无限期地运行(如果 Date.now 检查不存在)。控制台中记录的值为 true 事实确认未触发超时。它在队列中,等待 var run = true...console.log() block 终止。

<小时/>

对于您的示例,执行顺序为:

/* note: no two functions execute at same time */
GetSQLTable();
/* functions scheduled via setTimeout execute one by one */
GetSQLTable2(0, ...);
GetSQLTable2(1, ...);
GetSQLTable2(2, ...);
/* AJAX requests complete one by one, not necessarily in the order they started */
OnSuccess(2);
OnSuccess(0);
/* JavaScript thread could be idle during callbacks */
OnSuccess(1);

引用文献:

关于javascript - AJAX 以多线程方式运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27504393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com