Home >Web Front-end >JS Tutorial >Analysis of the role of async:false/true in Ajax requests

Analysis of the role of async:false/true in Ajax requests

亚连
亚连Original
2018-05-24 10:35:321703browse

This article mainly introduces the role of async:false/true in Ajax requests. It explains the specific functions and usage skills of async:false/true in Ajax requests based on the analysis of examples. Friends in need can refer to it

The example of this article analyzes the role of async:false/true in Ajax requests. Share it with everyone for your reference, the details are as follows:

test.html code:

<a href="javascript:void(0)" onmouseover="testAsync()">

asy.js code:

function testAsync(){
  var temp;
  $.ajax({
    async: false,
    type : "GET",
    url : &#39;tet.php&#39;,
    complete: function(msg){
      alert(&#39;complete&#39;);
    },
    success : function(data) {
      alert(&#39;success&#39;);
      temp=data;
    }
  });
  alert(temp+&#39;  end&#39;);
}

tet.php code:

<?php
  echo "here is html code";
  sleep(5);
?>

##async: false, (default is true) ;

As above: false means synchronization. The Ajax request in this testAsync() method locks the entire browser. Only after the execution of tet.php is completed, other operations can be performed.

When async: true, the ajax request is asynchronous. But there is a problem: the ajax request in testAsync() and the subsequent operations are executed asynchronously, so when tet.php has not been executed, the operations following the ajax request may have been executed.

For example:

alert(temp ' end');

However, the temp data is assigned after the ajax request success, and as a result, it will be empty when output.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax realizes three-level cascading of provinces and municipalities

Ajax simple implementation of uploading pictures and previewing them

Simple application based on Ajax form submission and background processing

##

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

Statement:
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