Sebastian Gomez
Using Alarms in Chrome Extensions
Chrome's chrome.alarms API lets us schedule code to run at specific intervals. Below I explain how you can use this API in your projects step by step.
Why we use alarms in Manifest V3
Before touching any code, it's worth understanding why this API exists. In Manifest V3 extensions no longer have a persistent background page, but a service worker that is ephemeral: the browser terminates it when there is nothing to do and reactivates it only when an event occurs. That means a setTimeout or a setInterval will not survive, because the service worker may have shut down long before the timer fires.
The chrome.alarms API solves exactly this problem: alarms are managed by the browser itself, so they persist even when the service worker sleeps and they wake it back up when the time comes. That is why it is the recommended mechanism for scheduling deferred or periodic work in modern extensions.
Creating alarms
To create an alarm we use the chrome.alarms.create method. This method accepts parameters such as the alarm name and the time interval.
Let's look at an example:
chrome.alarms.create('myAlarm', { delayInMinutes: 1, periodInMinutes: 1 });Here delayInMinutes says how long to wait before the first trigger, and periodInMinutes how often to repeat it afterward.
Note on the minimum interval: in published extensions, Chrome enforces a minimum interval of roughly 1 minute. If you configure values below that limit, they are silently ignored in production (they only work when loading the unpacked extension during development). That is why the example uses 1-minute values: do not try to schedule alarms of a few seconds expecting them to fire in a real extension.
Configuring permissions
Make sure to add the required permission in the manifest.json file:
{
"permissions": ["alarms"]
}Listening for alarms
To react to an alarm we use the chrome.alarms.onAlarm.addListener event:
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'myAlarm') {
console.log('Alarma activada');
}
});As you can see, we receive the alarm object and check its name to know which one fired. This is useful when you have several alarms and want to run different logic for each one.
3-point summary
- In Manifest V3 the service worker is ephemeral, so
setTimeoutandsetIntervalare not reliable for deferred work; we usechrome.alarms, which the browser manages. - We create alarms with
chrome.alarms.createand react to them withchrome.alarms.onAlarm.addListener, checkingalarm.namewhen we have several. - Remember to declare the
alarmspermission inmanifest.jsonand to respect the roughly 1-minute minimum interval in published extensions.
Suggested exercises
- Build a minimal extension that registers an alarm every minute and writes a message to the service worker console each time it fires.
- Add a second alarm with a different name and use
alarm.nameinside the listener to run a different action depending on which one fires. - Look into
chrome.alarms.getAllandchrome.alarms.clear, and implement a button that lists the active alarms and lets you cancel them.
I hope this post gave you a general overview of how you can use the chrome.alarms API to build useful and creative extensions. If you have ideas or projects in mind, don't hesitate to share them. Leave me a comment if it helped, and remember that if you liked it you can also share it using the social links below. That's all, I hope this post is useful to you and that you can apply it to a project you have in mind. Good luck.
Sebastian Gomez
Creador de contenido principalmente acerca de tecnología.