aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/serviceworker/state/index.html
blob: 68c0c7ca4e37aeac8ad8d789335c8d2dede1507c (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
---
title: ServiceWorker.state
slug: Web/API/ServiceWorker/state
translation_of: Web/API/ServiceWorker/state
---
<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>

<div>ServiceWorker接口的这个只读的<strong>state</strong>属性返回一个代表service worker当前状态的字符串。值可以是以下这些:installing, installed, activating, activated, 或者是redundant.</div>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox">someURL = ServiceWorker.state
</pre>

<h3 id="值"></h3>

<p>一个 {{domxref("ServiceWorkerState")}} 的定义值 (<a href="http://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-state-enum">see the spec</a>.)</p>

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

<p>这块代码出自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/registration-events/index.html">service worker registration-events sample</a> (<a href="https://googlechrome.github.io/samples/service-worker/registration-events/">live demo</a>). 代码监听了任何{{domxref("ServiceWorker.state")}}中的改变,并返回它的值.</p>

<pre class="brush: js">var serviceWorker;
if (registration.installing) {
  serviceWorker = registration.installing;
  document.querySelector('#kind').textContent = 'installing';
} else if (registration.waiting) {
  serviceWorker = registration.waiting;
  document.querySelector('#kind').textContent = 'waiting';
} else if (registration.active) {
  serviceWorker = registration.active;
  document.querySelector('#kind').textContent = 'active';
}

if (serviceWorker) {
  logState(serviceWorker.state);
  serviceWorker.addEventListener('statechange', function(e) {
  logState(e.target.state);
  });
}</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">附注</th>
  </tr>
  <tr>
   <td>{{SpecName('Service Workers', '#service-worker-state-attribute', 'state')}}</td>
   <td>{{Spec2('Service Workers')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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

{{Compat("api.ServiceWorker.state")}}