这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
AbortSignal
interface represents a signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an
AbortController
对象。
The AbortSignal interface also inherits properties from its parent interface,
EventTarget
.
AbortSignal.aborted
只读
布尔
that indicates whether the request(s) the signal is communicating with is/are aborted (
true
) 或不 (
false
).
Listen to this event using
addEventListener()
或通过把事件监听器赋值给
on
eventname
特性为此接口。
abort
onabort
特性。
The AbortSignal interface inherits methods from its parent interface,
EventTarget
.
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
对象使用
AbortController.signal
特性。
当
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 an
AbortError
.
Current version of Firefox rejects the promise with a
DOMException
You can find a full working example on GitHub — see abort-api ( see it running live also ).
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'AbortSignal' in that specification. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
AbortSignal
|
Chrome 66 | Edge 16 | Firefox 57 | IE No | Opera 53 | Safari 11.1 | WebView Android 66 | Chrome Android 66 | Firefox Android 57 | Opera Android 47 | Safari iOS 11.3 | Samsung Internet Android 9.0 |
abort
event
|
Chrome 66 | Edge 16 | Firefox 57 | IE No | Opera 53 | Safari 11.1 | WebView Android 66 | Chrome Android 66 | Firefox Android 57 | Opera Android 47 | Safari iOS 11.3 | Samsung Internet Android 9.0 |
aborted
|
Chrome 66 | Edge 16 | Firefox 57 | IE No | Opera 53 | Safari 11.1 | WebView Android 66 | Chrome Android 66 | Firefox Android 57 | Opera Android 47 | Safari iOS 11.3 | Samsung Internet Android 9.0 |
onabort
|
Chrome 66 | Edge 16 | Firefox 57 | IE No | Opera 53 | Safari 11.1 | WebView Android 66 | Chrome Android 66 | Firefox Android 57 | Opera Android 47 | Safari iOS 11.3 | Samsung Internet Android 9.0 |
完整支持
不支持
实验。期望将来行为有所改变。
AbortSignal
AbortController
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument