aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/abstractworker
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/abstractworker
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/abstractworker')
-rw-r--r--files/zh-cn/web/api/abstractworker/index.html87
-rw-r--r--files/zh-cn/web/api/abstractworker/onerror/index.html73
2 files changed, 160 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/abstractworker/index.html b/files/zh-cn/web/api/abstractworker/index.html
new file mode 100644
index 0000000000..53ee348af9
--- /dev/null
+++ b/files/zh-cn/web/api/abstractworker/index.html
@@ -0,0 +1,87 @@
+---
+title: AbstractWorker
+slug: Web/API/AbstractWorker
+tags:
+ - API
+ - AbstractWorker
+ - Interface
+ - Reference
+ - SharedWorker
+ - Web Workers
+ - Worker
+translation_of: Web/API/AbstractWorker
+---
+<p>{{ APIRef("Web Workers API") }}</p>
+
+<p> <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers API</a> 的 <strong><code>AbstractWorker</code></strong> 接口是一个定义适用于所有类型 worker 属性和方法的抽象接口,包括基础的 {{domxref("Worker")}},{{domxref("ServiceWorker")}} 以及 {{domxref("SharedWorker")}}。作为一个抽象类,你不能直接使用 <code>AbstractWorker</code>。</p>
+
+<h2 id="属性">属性</h2>
+
+<p><em><code>AbstractWorker</code></em><em> 接口不会继承任何属性。</em></p>
+
+<h3 id="事件处理函数">事件处理函数</h3>
+
+<dl>
+ <dt>{{domxref("AbstractWorker.onerror")}}</dt>
+ <dd>当 worker 中出现 {{domxref("ErrorEvent")}} 类型的错误时,{{ domxref("EventListener") }} 就会被调用。</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<p><em><code>AbstractWorker</code></em><em> 接口不会实现或继承任何方法。</em></p>
+
+<h2 id="例子">例子</h2>
+
+<p>作为一个抽象类接口,你不应该在代码中直接使用 <code>AbstractWorker</code> 接口。取而代之,你应该使用 {{domxref("Worker")}} 或 {{domxref("SharedWorker")}},这两者都继承了 <code>AbstractWorker</code> 的属性。</p>
+
+<p>下面的一小段代码是展示如何使用 {{domxref("Worker.Worker", "Worker()")}} 构造函数构造 {{domxref("Worker")}} 对象并使用它:</p>
+
+<pre class="notranslate"><code>var myWorker = new Worker("worker.js");
+
+first.onchange = function() {
+ myWorker.postMessage([first.value,second.value]);
+ console.log('Message posted to worker');
+}</code></pre>
+
+<p>worker 的代码会从 <code>"worker.js"</code> 文件被加载。代码假设这里已经有一个现成的 {{HTMLElement("input")}} 元素且由 <code>first</code> 表示;设置了一个用于 {{event("change")}} 事件的事件处理函数,所以当用户更改 <code>first</code> 的值时,一个提示信息会被提交且通知到 worker。</p>
+
+<p>完整的例子,请看:</p>
+
+<ul>
+ <li><a href="https://github.com/mdn/simple-web-worker">专用worker的基本例子</a> ( <a href="http://mdn.github.io/simple-web-worker/">运行专用worker</a> )</li>
+ <li><a href="https://github.com/mdn/simple-shared-worker">共享worker的基本例子</a> ( <a href="http://mdn.github.io/simple-shared-worker/">运行共享worker</a> )</li>
+</ul>
+
+<h2 id="相关规范">相关规范</h2>
+
+<table>
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("HTML WHATWG", "#abstractworker", "AbstractWorker")}}</td>
+ <td>{{Spec2("HTML WHATWG")}}</td>
+ <td>No change from {{SpecName("Web Workers")}}.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性"><br>
+ 浏览器兼容性</h2>
+
+<p>{{Compat("api.AbstractWorker")}}</p>
+
+<div id="compat-desktop"></div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{domxref("Worker")}},{{domxref("ServiceWorker")}} 以及 {{domxref("SharedWorker")}} 接口,都基于 <code>AbstractWorker</code> 定义</li>
+ <li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers API</a></li>
+ <li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">Using Web Workers</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/abstractworker/onerror/index.html b/files/zh-cn/web/api/abstractworker/onerror/index.html
new file mode 100644
index 0000000000..2fd61e2ccc
--- /dev/null
+++ b/files/zh-cn/web/api/abstractworker/onerror/index.html
@@ -0,0 +1,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>