Skip to content

Commit

Permalink
Update and modernize example #2.
Browse files Browse the repository at this point in the history
Simplify it a little but, use `await` instead of `.then()`. Also replace
`addEventListener()` with a direct assignment of the `onrelease` attribute
to make the example easier to find.

Fixes #250.
  • Loading branch information
rakuco committed Feb 7, 2020
1 parent ace16d3 commit 543bd10
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions index.html
Expand Up @@ -615,6 +615,10 @@ <h3>
lock</a>'s {{[[ActiveLocks]]}} internal slot. See <a>release a wake
lock</a>.
</aside>
<aside class="note">
[[[#onrelease-example]]] contains an example of how to use the
{{WakeLockSentinel/onrelease}} event handler.
</aside>
</section>
</section>
<section>
Expand Down Expand Up @@ -888,21 +892,14 @@ <h2>
on a checkbox, but updates the checkbox checked state in case the wake
lock state changes:
</p>
<pre class="example js">
<pre class="example js" id="onrelease-example">
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
document.body.appendChild(checkbox);

navigator.wakeLock.request("screen").then(lock =&gt; {
checkbox.checked = true;

const listener = (ev) =&gt; {
checkbox.checked = false;
ev.target.removeEventListener('release', listener);
};

lock.addEventListener('release', listener);
});
const sentinel = await navigator.wakeLock.request("screen");
checkbox.checked = true;
sentinel.onrelease = () =&gt; checkbox.checked = false;
</pre>
<p>
In this example, two different wake lock requests are created and
Expand Down

0 comments on commit 543bd10

Please sign in to comment.