AbortController

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

AbortController接口表示一个控制器对象,允许你根据需要中止一个或多个 Web请求。

你可以使用 AbortController.AbortController() 构造函数创建一个新的 AbortController 。使用AbortSignal 对象可以完成与与DOM请求的通信。

构造函数

AbortController.AbortController()
创建一个新的AbortController 对象实例。

属性

AbortController.signal 只读
返回一个AbortSignal对象实例,它可以用来 with/abort 一个Web(网络)请求。

方法

AbortController.abort()
中止一个尚未完成的Web(网络)请求。这能够中止fetch 请求,任何响应Body的消费者和流。

示例

在下面的代码片段中,我们想通过 Fetch API 下载一段视频。

我们先使用AbortController()构造函数创建一个控制器,然后使用AbortController.signal属性获取其关联 AbortSignal对象的引用。

当一个 fetch request 初始化,我们把 AbortSignal 作为一个选项传递到到请求对象(如下 {signal})。这将信号和控制器与获取请求相关联然后允许我们通过调用AbortController.abort()中止请求,如下第二个事件监听函数。

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;
  })
}

注意:abort() 被调用时,fetch() promise 拒绝一个名为 AbortError 的DOMException

可以在GitHub上找到完整的工作示例 — 请参见 abort-api另请参见实时运行)。

规范

Specification Status Comment
DOM
AbortController
Living Standard Initial definition

浏览器兼容

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
AbortController
Experimental
Chrome Full support 66Edge Full support 16Firefox Full support 57IE No support NoOpera Full support 53Safari Full support 12.1
Full support 12.1
Partial support 11.1
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 57Opera Android Full support 47Safari iOS Full support 12.2
Full support 12.2
Partial support 11.3
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
Samsung Internet Android Full support 9.0
AbortController() constructor
Experimental
Chrome Full support 66Edge Full support 16Firefox Full support 57IE No support NoOpera Full support 53Safari Full support 12.1
Full support 12.1
Partial support 11.1
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 57Opera Android Full support 47Safari iOS Full support 12.2
Full support 12.2
Partial support 11.3
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
Samsung Internet Android Full support 9.0
abort
Experimental
Chrome Full support 66Edge Full support 16Firefox Full support 57IE No support NoOpera Full support 53Safari Full support 12.1
Full support 12.1
Partial support 11.1
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 57Opera Android Full support 47Safari iOS Full support 12.2
Full support 12.2
Partial support 11.3
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
Samsung Internet Android Full support 9.0
signal
Experimental
Chrome Full support 66Edge Full support 16Firefox Full support 57IE No support NoOpera Full support 53Safari Full support 12.1
Full support 12.1
Partial support 11.1
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 57Opera Android Full support 47Safari iOS Full support 12.2
Full support 12.2
Partial support 11.3
Notes
Notes Even though window.AbortController is defined, it doesn't really abort fetch requests. See bug 174980.
Samsung Internet Android Full support 9.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

参见