JS中Ajax的同步和异步

ajax同步 : 意味着此时请求Server后,JS代码不再继续执行,等待Server返回后才继续往下执行。

ajax异步 : 意味着此时请求Server后,JS代码继续执行,不管Server什么时候返回。

var f;
$.ajax({
      type : "post",
      url : "cuoche/checkCuocheInfoExpireTime.do",
      async:false,  //使用同步的方式,true为异步方式
      data : {
        "carId" : carId
      },
      dataType : "json",
      success : function(data) {
        console.info(data);
        if(typeof(data.flag) == 'undefined' || data.flag == '' || data.flag == null){
          f =  false;
          return ;
        }else{
          f = true;
          return ;
        }
      } ,
      error : function(data) {
        f = false;
        return ;
      }
    });
var f;
$.ajax({
      type : "post",
      url : "cuoche/checkCuocheInfoExpireTime.do",
      async:true,  //使用异步的方式,true为异步方式
      data : {
        "carId" : carId
      },
      dataType : "json",
      success : function(data) {
        console.info(data);
        if(typeof(data.flag) == 'undefined' || data.flag == '' || data.flag == null){
          f =  false;
          return ;
        }else{
          f = true;
          return ;
        }
      } ,
      error : function(data) {
        f = false;
        return ;
      }
    });
原文地址:https://www.cnblogs.com/smileblogs/p/9564215.html