AbortController

这是 实验性技术
检查 浏览器兼容性表格 在生产中使用这之前。

AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.

You can create a new AbortController object using the AbortController.AbortController() constructor. Communicating with a DOM request is done using an AbortSignal 对象。

构造函数

AbortController()
创建新的 AbortController object instance.

特性

AbortController.signal 只读
返回 AbortSignal object instance, which can be used to communicate with/abort a DOM request.

方法

AbortController.abort()
Aborts a DOM request before it has completed. This is able to abort fetch requests , consumption of any response Body , and streams.

范例

In the following snippet, we aim to download a video using the 抓取 API .

We first create a controller using the AbortController() constructor, then grab a reference to its associated AbortSignal object using the AbortController.signal 特性。

When the fetch request is initiated, we pass in the AbortSignal as an option inside the request's options object (see {signal} , below). This associates the signal and controller with the fetch request and allows us to abort it by calling AbortController.abort() , as seen below in the second event listener.

var controller = new AbortController();
var signal = controller.signal;
var downloadBtn = document.querySelector('.download');
var 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;
  })
}
					

注意 : When abort() is called, the fetch() promise rejects with a DOMException named AbortError .

You can find a full working example on GitHub — see abort-api ( see it running live also ).

规范

规范 状态 Comment
DOM
The definition of 'AbortController' in that specification.
实时标准 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AbortController
Chrome 66 Edge 16 Firefox 57 IE No Opera 53 Safari 12.1
12.1
部分支持 11.1
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
WebView Android 66 Chrome Android 66 Firefox Android 57 Opera Android 47 Safari iOS 12.2
12.2
部分支持 11.3
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
Samsung Internet Android 9.0
AbortController() 构造函数
Chrome 66 Edge 16 Firefox 57 IE No Opera 53 Safari 12.1
12.1
部分支持 11.1
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
WebView Android 66 Chrome Android 66 Firefox Android 57 Opera Android 47 Safari iOS 12.2
12.2
部分支持 11.3
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
Samsung Internet Android 9.0
abort
Chrome 66 Edge 16 Firefox 57 IE No Opera 53 Safari 12.1
12.1
部分支持 11.1
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
WebView Android 66 Chrome Android 66 Firefox Android 57 Opera Android 47 Safari iOS 12.2
12.2
部分支持 11.3
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
Samsung Internet Android 9.0
signal
Chrome 66 Edge 16 Firefox 57 IE No Opera 53 Safari 12.1
12.1
部分支持 11.1
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
WebView Android 66 Chrome Android 66 Firefox Android 57 Opera Android 47 Safari iOS 12.2
12.2
部分支持 11.3
Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980 .
Samsung Internet Android 9.0

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。

另请参阅