blob: 1e89dd329698fd8c86a9e49daa8a7c4c4af51b3f (
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
|
---
title: ServiceWorkerRegistration.unregister()
slug: Web/API/ServiceWorkerRegistration/unregister
translation_of: Web/API/ServiceWorkerRegistration/unregister
---
<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>
<p>{{domxref("ServiceWorkerRegistration")}} 接口的 <code><strong>unregister</strong></code> 方法用于取消对service worker的注册并返回一个 {{jsxref("Promise")}}。没有找到注册时,这个 promise 返回 <code>false</code> ,否则,不论取消成功与否都返回 <code>true</code> (当其他人在同一作用域调用了 {{domxref("ServiceWorkerContainer.register")}} 可能取消失败)service worker 会在取消注册前完成一切正在进行的操作。</p>
<div class="note">
<p><strong>Note</strong>: 这一特性同样适用于 <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers</a>.</p>
</div>
<h2 id="Syntax" name="Syntax" style="line-height: 30px; font-size: 2.14285714285714rem;">语法</h2>
<pre class="syntaxbox" style="font-size: 14px;">ServiceWorkerRegistration.unregister().then(function(boolean) {
});</pre>
<h3 id="参数" style="line-height: 30px; font-size: 2.14286rem;">参数</h3>
<p>None.</p>
<h3 id="返回">返回</h3>
<p>Promise 返回一个bool值表示 service worker 是否被取消注册。</p>
<h2 id="例子" style="line-height: 30px; font-size: 2.14285714285714rem;">例子</h2>
<p>下面的简单例子中注册了一个service worker,然后立即取消了:</p>
<pre class="brush: js">if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
// registration worked
console.log('Registration succeeded.');
registration.unregister().then(function(boolean) {
// if boolean = true, unregister is successful
});
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
};</pre>
<h2 id="规范" style="line-height: 30px; font-size: 2.14285714285714rem;">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col"><font face="Open Sans, sans-serif"><span style="font-weight: normal;">Specification</span></font></th>
<th scope="col"><font face="Open Sans, sans-serif"><span style="font-weight: normal;">Status</span></font></th>
<th scope="col"><font face="Open Sans, sans-serif"><span style="font-weight: normal;">Comment</span></font></th>
</tr>
<tr>
<td>{{SpecName('Service Workers', '#service-worker-registration-unregister-method', 'ServiceWorkerRegistration.unregister()')}}</td>
<td>{{Spec2('Service Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.ServiceWorkerRegistration.unregister")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
<li><a href="https://github.com/mdn/sw-test">Service workers basic code example</a></li>
<li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
<li>{{jsxref("Promise")}}</li>
<li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li>
</ul>
|