In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to understand the difference between synchronous and asynchronous values of async attributes in ajax. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
The ajax method in jquery has an attribute async that controls synchronization and async, which defaults to true, that is, ajax requests default to asynchronous requests, and sometimes AJAX synchronization is used in projects. This synchronization means that when the JS code is loaded into the current AJAX, all the code in the page will stop loading, and the page will be suspended. When the AJAX is finished, it will continue to run other code pages. On the other hand, asynchronous AJAX code can also run when other code is running.
The attribute async in ajax is used to control how data is requested. The default is true, that is, data is requested asynchronously by default.
1. Async value is true (asynchronous)
When the ajax sends the request, while waiting for the server side to return, the foreground will continue to execute the script behind the ajax block until the server side returns the correct result, that is, two threads will be executed, one thread after the request from the ajax block and the script behind the ajax block (another thread)
For example
Ajax ({type: "POST", url: "Venue.aspx?act=init", dataType: "html", success:function (result) {/ / function1 () F1 (); f2 ();} failure:function (result) {alert ('Failed');},} function2 ()
In the above example, when the ajax block makes a request, he will stop function1 () and wait for the server to return, but at the same time (in the process of waiting), the foreground will execute function2 ().
Second, the async value is false (synchronization)
Execution of subsequent JS code is stopped when the current AJAX is executed, and execution of subsequent JS code cannot continue until the execution of the AJAX is complete.
For example
Ajax ({type: "POST", url: "Venue.aspx?act=init", dataType: "html", async: false, success:function (result) {/ / function1 () F1 (); f2 ();} failure:function (result) {alert ('Failed');},} function2 ()
When asyn is set to false, ajax's request is synchronized, that is, after the ajax block sends the request, he will wait at the function1 () place and will not execute function2 () until the function1 () part is finished.
The difference between synchronous and Asynchronous in Ajax
Var returnValue = null; xmlhttp = createXmlHttp (); xmlhttp.onreadystatechange = function () {if (xmlhttp.readyState = = 4 & & xmlhttp.status = = 200) {if (xmlhttp.responseText = = "true") {returnValue = "true";} else {returnValue = "false";}; xmlhttp.open ("Post", url,true); / / Asynchronous transfer xmlhttp.setRequestHeader ("If-Modified-Since", "0") / / do not cache Ajaxxmlhttp.send (sendStr); return returnValue
You can only use xmlHttpReq.onreadystatechange status values when you are asynchronous! Here are the different ways to call asynchronously and synchronously:
Java
XmlHttpReq.open ("GET", url,true); / / Asynchronous xmlHttpReq.onreadystatechange = showResult; / / showResult is the callback function name xmlHttpReq.send (null); function showResult () {if (xmlHttpReq.readyState = = 4) {if (xmlHttpReq.status = = 200) {*}
Java
XmlHttpReq.open ("GET", url,false); / / synchronous xmlHttpReq.send (null); showResult () / / showResult is a callback function name, but the specific usage is different ~ function showResult () {/ / if (xmlHttpReq.readyState = = 4) {not needed here, directly dosomething ~ / / if (xmlHttpReq.status = = 200) {* / / dosomething /} /} xmlhttp.open ("Post", url,true)
If it is synchronous (false), the return value is true or false, because after the execution of send, the program starts to execute onreadystatechange, and the program will not proceed to execute the next statement until the onreadystatechange has been executed and responseText has been obtained, so returnValue must have a value.
If it is asynchronous (true), the return value must be null, because the program will not wait for the response of xmlhttp after executing send, and then continue to execute the next statement, so the returnValue has already returned null before it comes and changes.
So if you want to get the xmlhttp return value, you must use synchronization, asynchronous can not get the return value.
Note when using xmlhttp pool synchronously and asynchronously: you can only create a new xmlhttp when you get xmlhttp, and you cannot extract the used xmlhttp from the pool. Because the readyState of the used xmlhttp is 4, synchronous and asynchronous will send but do not execute onreadystatechange.
On how to understand the async attribute value in ajax synchronous and asynchronous and synchronous and asynchronous differences to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.