请求:信号属性

基线 广泛可用

此功能已得到很好的建立,并且在许多设备和浏览器版本中都适用。自 2019 年 3 月.

只读的 signal 属性 Request 接口返回与请求关联的 AbortSignal

价值

一个 AbortSignal 对象。

示例

js
// Create a new abort controller
const controller = new AbortController();

// Create a request with this controller's AbortSignal object
const req = new Request("/", { signal: controller.signal });

// Add an event handler logging a message in case of abort
req.signal.addEventListener("abort", () => {
  console.log("abort");
});

// In case of abort, log the AbortSignal reason, if any
fetch(req).catch(() => {
  if (signal.aborted) {
    if (signal.reason) {
      console.log(`Request aborted with reason: ${signal.reason}`);
    } else {
      console.log("Request aborted but no reason was given.");
    }
  } else {
    console.log("Request not aborted, but terminated abnormally.");
  }
});

// Actually abort the request
controller.abort();

规范

规范
获取标准
# ref-for-dom-request-signal②

浏览器兼容性

BCD 表格仅在浏览器中加载