0%

722_ajax_重复请求问题

<script>
      var btns = document.querySelectorAll("button");
      let x;
      let isSending = false; //是否正在发送ajax请求
      btns[0].onclick = function () {
        //如果上一个请求正在发送,则取消这一次的请求
        if (isSending) {
          x.abort();
        }
        x = new XMLHttpRequest();
        //修改标识变量的值
        isSending = true;
        x.open("GET", "http://127.0.0.1:8000/delay");
        x.send();
        x.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            //判断响应状态码 2##表示成功
            if (xhr.status >= 200 && xhr.status < 300) {
              isSending = false;
            }
          }
        };
      };
    </script>