<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" />
</body>
</html>
<script>
const input = document.querySelector('input');
input.oninput = async (e) => {
let controlor = new AbortController();
//判断本次的输入时间和上次的输入时间间隔是否小于500ms
window.inputStamp = window.inputStamp || {
time: Date.now(),
count: 0,
};
if (
Date.now() - window.inputStamp.time < 2000 &&
window.inputStamp.count > 0
) {
controlor.abort('出错啦' + e.target.value);
window.inputStamp.time = Date.now();
} else {
window.inputStamp.time = Date.now();
window.inputStamp.count++;
// console.log(Date.now() - window.inputStamp.time,window.inputStamp.count,e.target.value);
}
console.log(window.inputStamp);
try {
let a = await fetch('http://127.0.0.1:3003', {
signal: controlor.signal,
})
.then((res) => res.json())
.then((res) => {
// console.log(Date.now() - window.inputStamp.time,window.inputStamp.count,e.target.value);
console.log(res);
});
} catch (e) {
console.log(controlor.signal.reason);
}
};
</script>