EventSource.onerror

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

EventSource 的属性 onerror 是当发生错误且这个错误事件(error)被 EventSource 触发时调用的一个事件处理函数。

语法

在类似 addEventListener() 这样的方法中使用事件名称,或设置事件处理器属性。

js
addEventListener("error", (event) => {});

onerror = (event) => {};

示例

js
evtSource.onerror = function () {
  console.log("EventSource failed.");
};

备注:你可以在 Github 上查看这个完整例子: Simple SSE demo using PHP.

规范

Specification
HTML Standard
# event-error
HTML Standard
# handler-eventsource-onerror

浏览器兼容性

BCD tables only load in the browser

参见