借助于 xhr 的 about() 方法,这样我们就可以在适当的位置去调用它,来取消请求。
1xhr.abort();
123456789101112//获取元素对象const btns = document.querySelectorAll('button');let xhr = null;btns[0].onclick = function() { xhr = new XMLHttpRequest(); xhr.open("GET", 'http://127.0.0.1:8000/delay'); xhr.send();}// abortbtns[1].onclick = function() { xhr.abort(); // --------------------------------------------}
对它的封装: —–传送门—–