--- title: 'Worker: messageerror event' slug: Web/API/Worker/messageerror_event translation_of: Web/API/Worker/messageerror_event ---
{{APIRef}}

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

是否冒泡
是否可取消
接口 {{domxref("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
{{SpecName('HTML WHATWG', 'indices.html#event-messageerror')}} {{Spec2('HTML WHATWG')}}

浏览器兼容性

{{Compat("api.Worker.messageerror_event")}}

另请参阅