diff options
Diffstat (limited to 'files/zh-cn/web/api/abortcontroller')
-rw-r--r-- | files/zh-cn/web/api/abortcontroller/abort/index.html | 86 | ||||
-rw-r--r-- | files/zh-cn/web/api/abortcontroller/abortcontroller/index.html | 86 | ||||
-rw-r--r-- | files/zh-cn/web/api/abortcontroller/index.html | 107 |
3 files changed, 279 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/abortcontroller/abort/index.html b/files/zh-cn/web/api/abortcontroller/abort/index.html new file mode 100644 index 0000000000..a37eda8176 --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/abort/index.html @@ -0,0 +1,86 @@ +--- +title: AbortController.abort() +slug: Web/API/AbortController/abort +translation_of: Web/API/AbortController/abort +original_slug: Web/API/FetchController/abort +--- +<div>{{APIRef("DOM")}}{{SeeCompatTable}}</div> + +<p>The <strong><code>abort()</code></strong> method of the {{domxref("AbortController")}} interface aborts a DOM request (e.g. a Fetch request) before it has completed. This is able to abort <a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch requests</a>, consumption of any response {{domxref("Body")}}, and streams.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="brush: js">controller.abort();</pre> + +<h3 id="Parameters">Parameters</h3> + +<p>None.</p> + +<h3 id="Return_value">Return value</h3> + +<p>Void.</p> + +<h2 id="Examples">Examples</h2> + +<p>In the following snippet, we aim to download a video using the <a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a>.</p> + +<p>We first create a controller using the {{domxref("AbortController.AbortController","AbortController()")}} constructor, then grab a reference to its associated {{domxref("AbortSignal")}} object using the {{domxref("AbortController.signal")}} property.</p> + +<p>When the <a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch request</a> is initiated, we pass in the <code>AbortSignal</code> as an option inside the request's options object (see <code>{signal}</code>, below). This associates the signal and controller with the fetch request and allows us to abort it by calling {{domxref("AbortController.abort()")}}, as seen below in the second event listener.</p> + +<pre class="brush: js">var controller = new AbortController(); +var signal = controller.signal; + +var downloadBtn = document.querySelector('.download'); +var abortBtn = document.querySelector('.abort'); + +downloadBtn.addEventListener('click', fetchVideo); + +abortBtn.addEventListener('click', function() { + controller.abort(); + console.log('Download aborted'); +}); + +function fetchVideo() { + ... + fetch(url, {signal}).then(function(response) { + ... + }).catch(function(e) { + reports.textContent = 'Download error: ' + e.message; + }) +}</pre> + +<div class="note"> +<p><strong>Note</strong>: When <code>abort()</code> is called, the <code>fetch()</code> promise rejects with an <code>AbortError</code>.</p> +</div> + +<p>You can find a full working example on GitHub — see <a href="https://github.com/mdn/dom-examples/tree/master/abort-api">abort-api</a> (<a href="https://mdn.github.io/dom-examples/abort-api/">see it running live also</a>).</p> + +<h2 id="Specifications">Specifications</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('DOM WHATWG', '#dom-abortcontroller-abort', 'abort()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.AbortController.abort")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a></li> +</ul> diff --git a/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html b/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html new file mode 100644 index 0000000000..036806e29a --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/abortcontroller/index.html @@ -0,0 +1,86 @@ +--- +title: AbortController.AbortController() +slug: Web/API/AbortController/AbortController +tags: + - AbortController + - Constructor + - Fetch +translation_of: Web/API/AbortController/AbortController +original_slug: Web/API/FetchController/AbortController +--- +<div>{{APIRef("DOM")}}{{SeeCompatTable}}</div> + +<p><strong><code>AbortController()</code></strong> 构造函数创建了一个新的<code>AbortController实例</code></p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="brush: js">var controller = new AbortController();</pre> + +<h3 id="Parameters">Parameters</h3> + +<p>无.</p> + +<h2 id="Examples">Examples</h2> + +<p>在下面的这段代码中, 我们将通过<a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a>来下载一段视频.</p> + +<p>首先通过{{domxref("AbortController.AbortController","AbortController()")}} 构造函数来创建一个controller实例, 然后通过{{domxref("AbortController.signal")}} 属性获取到它的关联对象{{domxref("AbortSignal")}} 的引用.</p> + +<p>当 <a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch request</a> 初始化后, 将 <code>AbortSignal</code> 作为一个选项传入请求的选项参数中 (如下 <code>{signal}</code>). 这将signal,controller与fetch请求关联起来, 允许我们通过调用{{domxref("AbortController.abort()")}}来取消fetch请求, 正如下第二个事件监听器所示.</p> + +<pre class="brush: js">var controller = new AbortController(); +var signal = controller.signal; + +var downloadBtn = document.querySelector('.download'); +var abortBtn = document.querySelector('.abort'); + +downloadBtn.addEventListener('click', fetchVideo); + +abortBtn.addEventListener('click', function() { + controller.abort(); + console.log('Download aborted'); +}); + +function fetchVideo() { + ... + fetch(url, {signal}).then(function(response) { + ... + }).catch(function(e) { + reports.textContent = 'Download error: ' + e.message; + }) +}</pre> + +<div class="note"> +<p><strong>提示</strong>: 当<code>abort()</code> 被调用, <code>fetch()</code> promise 将会抛出一个<code>AbortError对象</code>.</p> +</div> + +<p>你可以在GitHub上找到一个完整的使用示例 — see <a href="https://github.com/mdn/dom-examples/tree/master/abort-api">abort-api</a> (<a href="https://mdn.github.io/dom-examples/abort-api/">see it running live also</a>).</p> + +<h2 id="Specifications">Specifications</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('DOM WHATWG', '#dom-abortcontroller-abortcontroller', 'AbortController()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.AbortController.AbortController")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a></li> +</ul> diff --git a/files/zh-cn/web/api/abortcontroller/index.html b/files/zh-cn/web/api/abortcontroller/index.html new file mode 100644 index 0000000000..c29b4a0a7d --- /dev/null +++ b/files/zh-cn/web/api/abortcontroller/index.html @@ -0,0 +1,107 @@ +--- +title: AbortController +slug: Web/API/AbortController +tags: + - API + - AbortController + - Fetch + - how to cancel a fetch request +translation_of: Web/API/AbortController +original_slug: Web/API/FetchController +--- +<div>{{APIRef("DOM")}}{{SeeCompatTable}}</div> + +<p><strong><code>AbortController</code></strong>接口表示一个控制器对象,允许你根据需要中止一个或多个 Web请求。</p> + +<p>你可以使用 {{domxref("AbortController.AbortController()")}} 构造函数创建一个新的 <code>AbortController</code> 。使用{{domxref("AbortSignal")}} 对象可以完成与与DOM请求的通信。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("AbortController.AbortController()")}}</dt> + <dd><font><font>创建一个新的</font></font><code>AbortController</code> <font><font>对象实例。</font></font></dd> +</dl> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("AbortController.signal")}} {{readonlyInline}}</dt> + <dd><font><font>返回一个</font></font>{{domxref("AbortSignal")}}<font><font>对象实例,</font></font>它可以用来 with/abort 一个Web(网络)请求。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("AbortController.abort()")}}</dt> + <dd>中止一个尚未完成的Web(网络)请求。这能够中止<a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch </a>请求,任何响应{{domxref("Body")}}的消费者和流。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<p><font><font>在下面的代码片段中,我们想通过</font></font> <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a> 下载一段视频。</p> + +<p><font><font>我们先使用</font></font>{{domxref("AbortController.AbortController","AbortController()")}}构造函数<font><font>创建一个控制器,然后使用</font></font>{{domxref("AbortController.signal")}}属性<font><font>获取其关联</font></font> {{domxref("AbortSignal")}}对象<font><font>的引用。</font></font></p> + +<p>当一个 <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch request</a> 初始化,我们把 <code>AbortSignal</code> 作为一个选项传递到到请求对象(如下 <code>{signal}</code>)。这将信号和控制器与获取请求相关联然后允许我们通过调用{{domxref("AbortController.abort()")}}中止请求,如下第二个事件监听函数。</p> + +<pre class="brush: js notranslate">const controller = new AbortController(); +let signal = controller.signal; + +const downloadBtn = document.querySelector('.download'); +const abortBtn = document.querySelector('.abort'); + +downloadBtn.addEventListener('click', fetchVideo); + +abortBtn.addEventListener('click', function() { + controller.abort(); + console.log('Download aborted'); +}); + +function fetchVideo() { + //... + fetch(url, {signal}).then(function(response) { + //... + }).catch(function(e) { + reports.textContent = 'Download error: ' + e.message; + }) +}</pre> + +<div class="note"> +<p><strong>注意:</strong>当<code>abort()</code> 被调用时,<code>fetch()</code> promise 拒绝一个名为 <code>AbortError</code> 的<code>DOMException </code>。</p> +</div> + +<p>可以在GitHub上找到完整的工作示例 — 请参见 <a href="https://github.com/mdn/dom-examples/tree/master/abort-api">abort-api</a>(<a href="https://mdn.github.io/dom-examples/abort-api/">另请参见实时运行</a>)。</p> + +<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('DOM WHATWG', '#interface-abortcontroller', 'AbortController')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("api.AbortController")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a></li> + <li><a href="https://developers.google.com/web/updates/2017/09/abortable-fetch">Abortable Fetch</a> by Jake Archibald</li> +</ul> + +<div id="gtx-trans" style="position: absolute; left: 542px; top: 1890px;"> +<div class="gtx-trans-icon"></div> +</div> |