Skip to content

Commit

Permalink
Document device sleep and persistence behaviour of alarms API (Google…
Browse files Browse the repository at this point in the history
…Chrome#7776)

* Document device sleep and persistence behaviour of alarms API

* Address feedback

* Add example code

* Update site/en/docs/extensions/reference/alarms/index.md

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>

---------

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
  • Loading branch information
2 people authored and sebastianbenz committed Nov 14, 2023
1 parent 6bc2c15 commit c96ff1a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions site/en/docs/extensions/reference/alarms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ To use the `chrome.alarms` API, declare the `"alarms"` permission in the [manife
}
```

## Concepts and usage

### Device sleep

The behavior of alarms when a device goes to sleep is currently undefined. An alarm will never fire
early but may fire significantly later than expected if a device went to sleep after an alarm was
scheduled.

### Persistence

Alarms generally persist until an extension is updated. However, this is not guaranteed, and alarms
may be cleared when the browser is restarted. Consequently, consider setting a value in storage
when an alarm is created, and then ensure it exists each time your service worker starts up. For example:

```js
const STORAGE_KEY = "user-preference-alarm-enabled";

async function checkAlarmState() {
const { alarmEnabled } = await chrome.storage.get(STORAGE_KEY);

if (alarmEnabled) {
const alarm = await chrome.alarms.get("my-alarm");

if (!alarm) {
await chrome.alarms.create({ periodInMinutes: 1 });
}
}
}

checkAlarmState();
```


## Examples

The following examples show how to use and respond to an alarm. To try this API,
Expand Down

0 comments on commit c96ff1a

Please sign in to comment.