<分区>
我正在尝试理解 js 事件循环。 这是我的 js 代码:
var start = new Date().getTime();
var url = "/WebForm1.aspx/Test1";
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log('Test1, elapsed: ' + (new Date().getTime() - start) + 'ms');
},
});
url = "/WebForm1.aspx/Test2";
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log('Test2, elapsed: ' + (new Date().getTime() - start) + 'ms');
},
});
url = "/WebForm1.aspx/Test3";
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log('Test3, elapsed: ' + (new Date().getTime() - start) + 'ms');
},
});
这是我的服务器端代码:
[WebMethod]
public static void Test1()
{
System.Threading.Thread.Sleep(1500);
}
[WebMethod]
public static void Test2()
{
System.Threading.Thread.Sleep(2000);
}
[WebMethod]
public static void Test3()
{
System.Threading.Thread.Sleep(3000);
}
现在的结果是:
Test1, elapsed: 1542ms
Test3, elapsed: 4578ms
Test2, elapsed: 6636ms
现在我不明白的是,为什么他们不一起执行?是服务器端一次只能处理一个请求,还是跟js事件循环有关?