jQuery superimposes ajax and stops of terminates ajax requests

  • 2021-07-09 06:54:05
  • OfStack

The key code for jQuery to overlay and stop ajax is as follows:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>jQuery Termination in Ajax Request </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>
  <script>
    var arrayAJAX = new Array();
    $(function () {
      $("#btn").bind("click", function () {
        for (var i = 0; i < 100; i++) {
          starAJAX(i);
        }
      });
      $("#bt2").bind("click", function () {
        stopAJAX();
        alert(" Terminate AJAX Request ");
      });
    })
    // Stop ajax
    function stopAJAX() {
      for (var i = 0; i < arrayAJAX.length; i++) {
        arrayAJAX[i].abort();
      }
      arrayAJAX = new Array();
    }
    // Add ajax
    function starAJAX(i) {
      var options = {
        url: '/Home/addallrecommandbook',
        data: "html=" + i,
        success: function (data, textStatus) {
          if (textStatus == 'success') {
            alert(" Add successfully! ");
          }
          else {
          }
        },
        error: function (x, msg, err) {
        }
      };
      arrayAJAX.push($.ajax(options));
    }
  </script>
</head>
<body>
  <input type="button" id="btn" value="starAJAX" />
  <input type="button" id="bt2" value="stopAjax" />
</body>
</html>

Related articles: