{async:false}如何在jQuery AJAX请求中起作用?

时间:2022-10-07 14:36:37

I know why and how to use {async:false} in jQuery AJAX request.

我知道为什么以及如何在jQuery AJAX请求中使用{async:false}。

But what I need is how this works synchronously? What is the magic behind this?

但我需要的是它是如何同步工作的?这背后的魔力是什么?

2 个解决方案

#1


4  

Because the native XMLHTTPRequest object provides the possibility to make synchronous requests:

因为本机XMLHTTPRequest对象提供了进行同步请求的可能性:

async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.

async可选的布尔参数,默认为true,指示是否异步执行操作。

You can assume that it does that by pausing the thread in which JS runs.

你可以假设它通过暂停JS运行的线程来做到这一点。

#2


0  

My best guess, is that when async is false, then it will wait until the readyState is set to DONE, which as defined by the W3, is:

我最好的猜测是,当async为false时,它将等待readyState设置为DONE,如W3所定义的那样:

The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

传输过程中数据传输已完成或出现问题(例如无限重定向)。

So basically when the request is completed, it will continue on.

所以基本上当请求完成时,它将继续。

#1


4  

Because the native XMLHTTPRequest object provides the possibility to make synchronous requests:

因为本机XMLHTTPRequest对象提供了进行同步请求的可能性:

async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.

async可选的布尔参数,默认为true,指示是否异步执行操作。

You can assume that it does that by pausing the thread in which JS runs.

你可以假设它通过暂停JS运行的线程来做到这一点。

#2


0  

My best guess, is that when async is false, then it will wait until the readyState is set to DONE, which as defined by the W3, is:

我最好的猜测是,当async为false时,它将等待readyState设置为DONE,如W3所定义的那样:

The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

传输过程中数据传输已完成或出现问题(例如无限重定向)。

So basically when the request is completed, it will continue on.

所以基本上当请求完成时,它将继续。