Jquery ajax attribute async of synchronous asynchronous example

  • 2020-03-26 23:00:47
  • OfStack

Jquery +ajax/" target="_blank"> Jquery ajax synchronization

$.ajax({
url : 'test.php',
type : 'post',
async: false,//Using synchronous mode,true is asynchronous
data : {'act':'addvideo', 'videoname':videoname},//Json objects are used here
success : function(data){
//code here...
},
fail:function(){
//code here...
}
});

Case 2

//javascript
function test()
{
 var a= 1;
 $.ajax({
  type   : 'GET',
  url    : 'test.php',
  data   : 'page=112',
  success:function(msg)
  {
   alert(msg);
   a= msg;
  }
 })
 alert(a);
}
//test.php
sleef('5'); //Take a five-minute break.
echo 'in';

 

Here, async default value is true, this kind of circumstance to asynchronously, that is to say after the ajax request, waiting for the server end to return to the process, the front desk will continue to perform ajax block at the back of the script, until the server side will return the correct results to execute the success, that is to say this is two threads of execution, after an ajax request block a thread and ajax block at the back of the script (another thread) :
Example 3

$.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, it stops at function1() and waits for the server side to return, but at the same time (during this wait), the foreground goes to function2(), that is, two threads appear, let's say function1() and function2().
When asyn is set to false, the ajax request is synchronous, that is, after the ajax block makes the request, it will wait in function1() and not execute function2() until the function1() part is finished.
Pay attention to
Synchronization means that when the JS code is loaded into the current AJAX, all the code in the page will be stopped and the page will be suspended in suspended animation. When the AJAX is finished, it will continue to run other code and the page will be suspended in suspended animation.
Asynchrony, on the other hand, allows the rest of the code to run while the AJAX code is running.
Async: false in jquery

Related articles: