blob: 2fd61e2cccb2a62d39c823bbe52b7c275a62a946 (
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
|
---
title: AbstractWorker.onerror
slug: Web/API/AbstractWorker/onerror
tags:
- API
- AbstractWorker
- EventHandler
- Property
- Reference
- Web Workers
- Workers
- onerror
translation_of: Web/API/AbstractWorker/onerror
---
<p>{{ APIRef("Web Workers API") }}</p>
<h2 id="概述">概述</h2>
<p>{{domxref("AbstractWorker")}}接口的<strong><code>onerror</code></strong>特性是一个事件句柄,在 {{domxref("Worker")}}的{{event("error")}}事件触发并冒泡时执行。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">myWorker.onerror = function() { ... };</pre>
<h2 id="示例">示例</h2>
<p>下面的代码片段展示了通过 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Worker.Worker" title="The Worker() constructor creates a Worker that executes the script at the specified URL. This script must obey the same-origin policy."><code>Worker()</code></a> 创建 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Worker" title="Worker 是可以在后台运行的任务,它能够被轻松的创建,还能向它的创建者发送消息。 你只要调用 Worker() 构造函数,指定一个需要运行在 worker 线程内的脚本,就能够很容易的创建一个 worker。"><code>Worker</code></a> 对象的过程, 以及设置onerror回调函数:</p>
<pre class="brush: js">var myWorker = new Worker("worker.js");
myWorker.onerror = function() {
console.log('There is an error with your worker!');
}</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', "#handler-abstractworker-onerror", "AbstractWorker.onerror")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>No change from {{SpecName("Web Workers")}}.</td>
</tr>
<tr>
<td>{{SpecName('Web Workers', "#handler-abstractworker-onerror", "AbstractWorker.onerror")}}</td>
<td>{{Spec2('Web Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.AbstractWorker.onerror")}}</p>
<h3 id="试图加载跨域_Worker_的错误差异">试图加载跨域 Worker 的错误差异</h3>
<p>早期浏览器会抛出 <code>SecurityError</code>,在规范变更后,则是 {{event("error")}} 事件。具体见 <a href="https://www.fxsitecompat.com/en-CA/docs/2016/loading-cross-origin-worker-now-fires-error-event-instead-of-throwing-worker-in-sandboxed-iframe-no-longer-allowed/">Loading cross-origin worker now fires error event instead of throwing; worker in sandboxed iframe no longer allowed</a>.</p>
</div>
<div id="compat-mobile"> </div>
<h2 id="另请参阅">另请参阅</h2>
<ul>
<li>The {{domxref("AbstractWorker")}} interface it belongs to.</li>
</ul>
|