Home Web Front-end Front-end Q&A What is the role of async in ajax

What is the role of async in ajax

Dec 23, 2021 am 11:39 AM
ajax

Async in ajax is used to set the execution of scripts in asynchronous or synchronous mode; when the async attribute value is true by default, it is asynchronous mode. After "$.Ajax" is executed, the script following ajax will continue to be executed. When its attribute value When set to false, the synchronous request will lock the browser, and other user operations must wait for the request to be completed.

What is the role of async in ajax

The operating environment of this article: windows10 system, javascript1.8.5&&html5 version, Dell G3 computer.

What is the role of async in ajax

async. The default is true, which is asynchronous mode. After $.Ajax is executed, it will continue to execute. The script behind ajax triggers the success method in $.Ajax until the server returns data. At this time, two threads are executed. If you set it to false, all requests are synchronous requests. Before returning a value, synchronous requests will lock the browser, and the user must wait for the request to complete other operations before they can be executed.

Check out an example below:

var temp;
$.ajax({
async: false,
type : "POST",
 url : defaultPostData.url,
  dataType : 'json',
   success : function(data) 
   {
       temp=data;
    }
 });
 alert(temp);
Copy after login

This ajax request is a synchronous request, and alert(temp) will not be executed until there is no return value.

If async is set to: true, it will not wait for the result returned by the ajax request, but will directly execute the statement following ajax.

However, for the above method of setting synchronous requests, some netizens once reported that after setting async to false, the original intention was to return the data and then execute the script behind $.Ajax. Unexpectedly, this place caused the problem in Firefox. A splash screen appears under the browser (Firefox 11.0), and ajax is triggered when the scroll bar is pulled down to the bottom. In the end, you can only comment out async:false, that is, when async is true, you successfully solved the problem of the Firefox browser scroll bar being pulled down to the bottom and triggering ajax to appear as a splash screen.

[Related tutorial recommendations: AJAX video tutorial]

The above is the detailed content of What is the role of async in ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

Understanding Ajax Frameworks: Explore Five Common Frameworks Understanding Ajax Frameworks: Explore Five Common Frameworks Jan 26, 2024 am 09:28 AM

Understanding Ajax Frameworks: Explore Five Common Frameworks

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

Asynchronous data exchange using Ajax functions

What are the ajax versions? What are the ajax versions? Nov 22, 2023 pm 02:00 PM

What are the ajax versions?

See all articles