如何取消一个 Ajax 请求
jQuery
XMLHttpRequest.abort()
https://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.send();
if (OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE) {
xhr.abort();
}
Axios
Fetch
Promise 无法取消(old)
Promise 可以取消了(AbortController)
AbortController
https://developer.mozilla.org/zh-CN/docs/Web/API/FetchController
const controller = new AbortController();
let signal = controller.signal;
const downloadBtn = document.querySelector('.download');
const abortBtn = document.querySelector('.abort');
downloadBtn.addEventListener('click', fetchVideo);
abortBtn.addEventListener('click', function() {
controller.abort();
console.log('Download aborted');
});
function fetchVideo() {
//...
fetch(url, {signal}).then(function(response) {
//...
}).catch(function(e) {
reports.textContent = 'Download error: ' + e.message;
})
}
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!