二十一(6)、Ajax中的同步异步编程 ------ 2020-

2020-04-11  本文已影响0人  自己写了自己看

1、异步状态的AJax

 let xhr = new XMLHttpRequest();

  xhr.open('GET', './data.json');
  
  xhr.onreadystatechange = function () {
      console.log(xhr.readyState); // 一次输出 2、3、4
  }

  xhr.send(null);

2、同步状态的Ajax

 let xhr = new XMLHttpRequest();

  xhr.open('GET', './data.json', false);
  
  xhr.onreadystatechange = function () {
      console.log(xhr.readyState); // 只能输出4
  }

  xhr.send(null);

  // 同步状态下, 只有当xhr.readyState=4的时候,主线
  // 程才能空闲下来,去执行onreadystatechange事件,
  // 所以只能输出一次4;
上一篇 下一篇

猜你喜欢

热点阅读