网络异常与网络断开,发送AJAX处理
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> #result { width: 300px; height: 100px; border: 1px solid red; } </style> </head> <body> <button>点击发送</button> </body> <script> //当网络存在延迟/异常的特殊处理 const btm = document.getElementsByTagName('button')[0]; let x =null; let isSending = false;//是否正在发送AJAX请求 true 是 false否 btm.addEventListener('click',function(){ if(isSending){//如果正在发送请求则继续请求,在新建一个请求 x.abort() }; x= new XMLHttpRequest(); isSending = true; x.open('GET','http://127.0.0.1:8000/delly'); x.send(); if(x.onreadystatechange === 4){ //修改变量标识 isSending = false; } }) </script> </html>