async
The default is true
, which is asynchronous mode. After $.Ajax
is executed, the script behind ajax will continue to be executed until the server returns data. Finally, the success method in $.Ajax
is triggered. At this time, two threads are executed. If you set it to false
, all requests are synchronous requests. Before there is no return value, the synchronous request will lock the browser, and the user other operations must wait for the request to be completed. before it can be executed.
What are the specific examples of this other operation? Does it refer to http request or js script?
First picture
When
Whenasync
isfalse
, the code is blocked until the ajax call returns, sodone
(i.e.success
) is executed first and then the sentenceconsole.log
after the ajax call is executed.async
istrue
(default), the code is not blocked, so the followingconsole.log
is executed first, and then theconsole.log
indone
is executed after ajax returns.指js脚本
http://transcoder.tradaquan.com/from=2001a/bd_page_type=1/ssid=0/uid=0/pu=usm%401%2Csz%401320_2003%2Cta%40iphone_1_10.3_1_11.5/baiduid=3ECCA1E6D2665DB48EFEBB60D9D9084F/w=0_10_/t=iphone/l=3/tc?ref=www_iphone&lid=8260698868132866872&order=1&fm=alhm&h5ad=1&srd=1&dict=32&tj=h5_mobile_1_0_10_title&w_qd=IlPT2AEptyoA_yivDVKcCTpsvgzWOeIntjcXa3jSqfgrUO_&sec=21456&di=8e54227838fdccf9&bdenc=1&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IEQGG_ytK1DK6mlrte4viZQRAVDb6QHOTCU8sumX0sqdFtXLR_7Mi8xR_qbIwdzZz
$.Ajax
The js script behind. (Better not to use the word script)If ajax is synchronous, it means that js is executed sequentially, http requests are sent by ajax, and js is js. Don’t confuse them together.
Other user operations should refer to the user triggering js-related operations. If there is time to bind an element behind ajax, it can be triggered.
The final summary is that when the current js script is synchronous ajax, the script is executed sequentially; when asynchronous ajax is used, it is executed asynchronously. The so-called asynchronous execution means that when ajax is executed, the js statement after ajax is directly executed without waiting for the request to return.