Pianificare attività con la Alarms API
Eseguite in modo affidabile attività in background periodiche e posticipate in un service worker Manifest V3 usando chrome.alarms.
Pianificare attività con la Alarms API è una lezione Browser Extensions Development (Chrome & Edge) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Browser Extensions Development (Chrome & Edge), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Browser Extensions Development (Chrome & Edge) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.createwith delay or period - Handle
onAlarmregistered 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.
Domande Frequenti
La lezione «Pianificare attività con la Alarms API» è gratuita?
Sì — il testo completo di «Pianificare attività con la Alarms API» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Browser Extensions Development (Chrome & Edge), passa a CoddyKit PRO. Il corso Browser Extensions Development (Chrome & Edge) include 4 lezioni in totale.
Cosa imparerò in «Pianificare attività con la Alarms API»?
Eseguite in modo affidabile attività in background periodiche e posticipate in un service worker Manifest V3 usando chrome.alarms. Eserciti Browser Extensions Development (Chrome & Edge) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Browser Extensions Development (Chrome & Edge)?
Non è richiesta alcuna esperienza precedente. Browser Extensions Development (Chrome & Edge) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Pianificare attività con la Alarms API»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Browser Extensions Development (Chrome & Edge)?
Sì. Ogni lezione Browser Extensions Development (Chrome & Edge) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Gestione e controllo delle schede
- Invio programmatico dei moduli
- Automazione delle interazioni degli utenti
- Pianificare attività con la Alarms API