0Pricing
Browser Extensions Development (Chrome & Edge) · Lección

Programación de tareas con la API Alarms

Ejecute de forma fiable tareas periódicas y trabajos en segundo plano retrasados en un service worker de Manifest V3 mediante chrome.alarms.

Programación de tareas con la API Alarms es una lección gratuita de Browser Extensions Development (Chrome & Edge) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Browser Extensions Development (Chrome & Edge), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Browser Extensions Development (Chrome & Edge) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Why Not setInterval

In Manifest V3 the service worker unloads when idle, so setTimeout and setInterval are unreliable for scheduled work. The alarms API survives this by waking the worker when it is time to run.

Declaring the Permission

Add the alarms permission to the manifest before using the API.

{
  "permissions": ["alarms"]
}

Creating a One-Time Alarm

Use alarms.create with delayInMinutes for a single delayed task.

chrome.alarms.create('cleanup', { delayInMinutes: 5 })

Creating a Repeating Alarm

Add periodInMinutes to make the alarm fire again and again on a schedule.

chrome.alarms.create('sync', { periodInMinutes: 30 })

Handling the Alarm

Listen for onAlarm at the top level of your service worker. The alarm object tells you which one fired.

chrome.alarms.onAlarm.addListener((alarm) => {
  if (alarm.name === 'sync') {
    doSync()
  }
})

Register Listeners at the Top Level

Because the worker restarts, register onAlarm synchronously when the script first runs, not inside an async callback, or the wake-up may be missed.

// top of background.js, runs on every wake
chrome.alarms.onAlarm.addListener(handleAlarm)

Minimum Period

For packed extensions the smallest repeating period is about one minute. Shorter intervals are clamped, so do not rely on second-level timing.

chrome.alarms.create('tick', { periodInMinutes: 1 })

Inspecting Alarms

Use alarms.get or alarms.getAll to check what is scheduled, useful for avoiding duplicates.

const a = await chrome.alarms.get('sync')
if (!a) chrome.alarms.create('sync', { periodInMinutes: 30 })

Clearing Alarms

Remove a single alarm with clear or all of them with clearAll when a feature is turned off.

chrome.alarms.clear('sync')
chrome.alarms.clearAll()

Recreating on Install

Set up your alarms in the onInstalled event so they exist from the moment the extension is installed or updated.

chrome.runtime.onInstalled.addListener(() => {
  chrome.alarms.create('sync', { periodInMinutes: 30 })
})

Keeping Work Short

The worker may unload soon after the alarm. Keep the task quick, persist state to storage, and avoid long-running loops that could be cut off.

Quick Check

Test your alarms knowledge.

Recap

You learned reliable scheduling:

  • Use alarms.create with delay or period
  • Handle onAlarm registered at the top level
  • Respect the ~1 minute minimum period
  • Inspect, clear, and recreate alarms
  • Keep alarm work short

The alarms API makes background scheduling dependable under Manifest V3.

Preguntas frecuentes

¿La lección «Programación de tareas con la API Alarms» es gratis?

Sí — el texto completo de «Programación de tareas con la API Alarms» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Browser Extensions Development (Chrome & Edge), actualiza a CoddyKit PRO. El curso de Browser Extensions Development (Chrome & Edge) incluye 4 lecciones en total.

¿Qué aprenderé en «Programación de tareas con la API Alarms»?

Ejecute de forma fiable tareas periódicas y trabajos en segundo plano retrasados en un service worker de Manifest V3 mediante chrome.alarms. Practicas Browser Extensions Development (Chrome & Edge) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Browser Extensions Development (Chrome & Edge)?

No se requiere experiencia previa. Browser Extensions Development (Chrome & Edge) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Programación de tareas con la API Alarms»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Browser Extensions Development (Chrome & Edge)?

Sí. Cada lección de Browser Extensions Development (Chrome & Edge) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Gestión y control de pestañas
  2. Envío programático de formularios
  3. Automatización de interacciones de usuario
  4. Programación de tareas con la API Alarms
← Volver a Browser Extensions Development (Chrome & Edge)