Worker: messageerror event

Worker 对象接收到一条无法被反序列化的消息时, messageerror 事件将在该对象上被触发。

是否冒泡
是否可取消
接口 MessageEvent
对应事件处理属性 onmessageerror

例子

创建一个 worker ,使用 addEventListener() 监听 message 和 messageerror 事件:

// inside main.js

const worker = new Worker("static/scripts/worker.js");

worker.addEventListener("message", (event) => {
    console.error(`Received message from worker: ${event}`);
});

worker.addEventListener("messageerror", (event) => {
    console.error(`Error receiving message from worker: ${event}`);
});

同样,可以使用 onmessageerror 事件处理属性监听事件:

// inside main.js

const worker = new Worker("static/scripts/worker.js");

worker.onmessage = (event) => {
    console.error(`Received message from worker: ${event}`);
};

worker.onmessageerror = (event) => {
    console.error(`Error receiving message from worker: ${event}`);
};

规范

Specification Status
HTML Living Standard Living Standard

浏览器兼容性

BCD tables only load in the browser

另请参阅