Home > Web Front-end > JS Tutorial > body text

The role of async:false/true (synchronous/asynchronous) in Ajax requests

青灯夜游
Release: 2018-10-12 16:57:26
forward
2678 people have browsed it

This article will introduce to you the role of async:false/true (synchronous/asynchronous) in Ajax requests. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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, triggering $. The success method in Ajax is executed by two threads at this time. 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.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit AJAX Video Tutorial!

Related recommendations:

AJAX Online Manual

##ajax basic video tutorial

The above is the detailed content of The role of async:false/true (synchronous/asynchronous) in Ajax requests. For more information, please follow other related articles on the PHP Chinese website!

source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!