<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>TestAjax</title> <style> #result{ width: 200px; height: 100px; border: solid 1px #2a5caa; } </style></head><body> <button>发送请求</button> <script> const btns = document.querySelectorAll('button'); let x = null; let isSending = false;// 默认为False,没有在发请求 btns[0].onclick = function (){ if(isSending) x.abort();// 判断请求是否在发送,若正在发送,则取消,再次创建一个新的对象 x = new XMLHttpRequest(); isSending = true; x.open("GET",'http://localhost:8000/cancel') x.send(); x.onreadystatechange =function (){ if (x.readyState === 4 ){ isSending = false; } } } </script></body></html>