<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>重复请求问题</title>
</head>
<body>
<button>点击发送</button>
<script>
const btns = document.querySelectorAll('button');
let x = null;
let isSending = false;
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(x.readyState === 4){
isSending = false;
}
}
}
btns[1].onclick = function(){
x.abort();
}
</script>
</body>
</html>
const express = require('express');
const app = express();
app.all('/delay', (request, response) => {
response.setHeader('Access-Control-Allow-Origin', '*');
setTimeout(() => {
response.send('延时响应');
}, 3000)
});
app.listen(8000, ()=>{
console.log("服务已经启动, 8000 端口监听中....");
});