aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/worker/messageerror_event/index.html
blob: 32ebd5d631c82f5744660cb6027f4882c372d411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
title: 'Worker: messageerror event'
slug: Web/API/Worker/messageerror_event
translation_of: Web/API/Worker/messageerror_event
---
<div>{{APIRef}}</div>

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

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">是否冒泡</th>
   <td></td>
  </tr>
  <tr>
   <th scope="row">是否可取消</th>
   <td></td>
  </tr>
  <tr>
   <th scope="row">接口</th>
   <td>{{domxref("MessageEvent")}}</td>
  </tr>
  <tr>
   <th scope="row">对应事件处理属性</th>
   <td><code><a href="/en-US/docs/Web/API/Worker/onmessageerror">onmessageerror</a></code></td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>创建一个 worker ,使用 <code><a href="/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener()</a></code> 监听 <code>message</code> 和 <code>messageerror</code> 事件:</p>

<pre class="brush: js">// inside main.js

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

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

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

<p>同样,可以使用 <code><a href="/en-US/docs/Web/API/WindowEventHandlers/onmessageerror">onmessageerror</a></code> 事件处理属性监听事件:</p>

<pre class="brush: js">// inside main.js

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

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

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

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'indices.html#event-messageerror')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Worker.messageerror_event")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/API/Worker/postMessage">Worker.postMessage()</a></code></li>
 <li>相关事件: <code><a href="/docs/Web/API/Worker/message_event">message</a></code>.</li>
</ul>