AJAX 以多线程方式运行

AJAX acting in a multi threaded manner

本文关键字:运行 方式 多线程 AJAX      更新时间:2023-09-26

我知道JavaScript是单线程的(正如这个问题所解释的:如果Javascript不是多线程的,有什么理由实现异步Ajax 队列吗?(,但是我试图理解它如何应用于我开发的应用程序。 请参阅下面的代码:

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()块终止。


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

/* 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);

引用:

  • http://ejohn.org/blog/how-javascript-timers-work/
  • http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/