The async ajax attributes (synchronous and asynchronous control)

ajax async attribute for controlling the synchronous and asynchronous.

true (asynchronous request, default). It means that AJAX code to run when other code in the same run.

flase (synchronous). Means that when the JS code is loaded into the current AJAX pages when all the code will stop loading, the page will be suspended animation, will continue to run when AJAX is finished after other code, page lifted state of suspended animation.

$('input[type=button]').click(function(){
  $.ajax({
    url:'/test',
    type:'get',
    data:data,
    async:true,
    success:function(res){
      alert('success');
    },
error:function(err){      alert(
'err');     }   })   alert('after click') })

 

For example, the above code:
when async set to true, the button click, will first alert ( 'after click'), after the request is completed before alert ( 'success').
When async is set to false, click on the button, it will cause obstruction, stop the following code execution, only executing the ajax request, only to execute the code behind.

Guess you like

Origin www.cnblogs.com/gwxppg/p/11207737.html