- axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="axios.min.js"></script>
</head>
<body>
<script>
let cancel
axios.get('http://localhost', {
cancelToken: new axios.CancelToken(c => cancel = c)
}).then(res => {
console.log(1)
}).catch(e => {
console.log('请求异常')
})
document.onclick = () => {
cancel()
console.log('已经取消了')
}
</script>
</body>
</html>
- fetch
<script>
const controller = new AbortController()
fetch('http://localhost', {
signal: controller.signal
})
document.onclick = () => {
controller.abort()
}
</script>
网友评论