Home >Web Front-end >JS Tutorial >When to use AJAX asynchronous or synchronous?
AJAX stands for Asynchronous JavaScript and XML and is a technology that allows web pages to be updated asynchronously, meaning that the browser does not need to reload the entire page when only a small portion of the data on the page changes. . AJAX only passes updated information between servers.
Standard web applications handle interactions between web visitors and servers synchronously. This means one thing happens right after the other; the server doesn't multitask. If the button is clicked, a message is sent to the server and a response is returned. You cannot interact with any other page elements until the response is received and the page is updated.
Obviously, this delay can negatively impact the web visitor's experience - hence, AJAX.
What is AJAX?
AJAX is not a programming language, but a client-side script that combines communication with a web server (i.e. in the user's browser technology of running scripts). Additionally, its name is a bit misleading: while an AJAX application might use XML to send data, it could also just use plain text or JSON text. But usually, it uses the XMLHttpRequest object in the browser to request data from the server and uses JavaScript to display the data.
AJAX: synchronous or asynchronous
AJAX can access the server synchronously and asynchronously:
synchronously, where the script stops and waits The server sends back a response before continuing.
Asynchronous mode, where the script is allowed to continue processing the page and handles the response as it arrives.
Handling requests synchronously is similar to reloading the page, but only downloads the requested information instead of downloading the entire page. Therefore, using AJAX synchronously is much faster than not using it at all - but it still requires the visitor to wait for the download to complete before proceeding with any further interaction with the page. Everyone knows they sometimes have to wait for a page to load, but most people aren't used to significant delays continuing once they're on the site.
Handling your request asynchronously avoids delays in retrieving from the server because your visitors can continue to interact with the web page; the requested information will be processed in the background, and the response will update the page as it arrives. Furthermore, even if the response is delayed (for example, in the case of very large data), site visitors may not realize it because they are occupied elsewhere on the page.
Therefore, the preferred way to use AJAX is to use asynchronous calls whenever possible. This is the default setting in AJAX.
Why use synchronous AJAX?
If asynchronous calls provide such an improved user experience, then why does AJAX provide a way to make synchronous calls?
While asynchronous calls are the best option in most cases, there are rare cases where it doesn't make sense to allow a visitor to continue interacting with a web page until a specific server-side process is complete.
In many cases it is better not to use AJAX at all, but to reload the entire page. The synchronous option in AJAX is useful in rare cases where you cannot use asynchronous calls, but there is no need to reload the entire page. For example, you may need to handle some transactions where order is important. Consider a situation where a web page needs to return a confirmation page after the user clicks something. This task requires synchronous requests.
Related recommendations: "ajax tutorial"
The above is the detailed content of When to use AJAX asynchronous or synchronous?. For more information, please follow other related articles on the PHP Chinese website!