Alarms APIによるタスクのスケジュール設定
chrome.alarmsを使い、Manifest V3のサービスワーカーで定期的または遅延実行するバックグラウンド処理を確実に実行します。
「Alarms APIによるタスクのスケジュール設定」はCoddyKit上の無料Browser Extensions Development (Chrome & Edge)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBrowser Extensions Development (Chrome & Edge)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Browser Extensions Development (Chrome & Edge)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「Alarms APIによるタスクのスケジュール設定」レッスンは無料ですか?
はい。「Alarms APIによるタスクのスケジュール設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Browser Extensions Development (Chrome & Edge)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Browser Extensions Development (Chrome & Edge)コースには全4レッスンが含まれています。
「Alarms APIによるタスクのスケジュール設定」で何を学びますか?
chrome.alarmsを使い、Manifest V3のサービスワーカーで定期的または遅延実行するバックグラウンド処理を確実に実行します。 ブラウザで直接実行するハンズオンコードでBrowser Extensions Development (Chrome & Edge)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Browser Extensions Development (Chrome & Edge)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBrowser Extensions Development (Chrome & Edge)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Alarms APIによるタスクのスケジュール設定」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBrowser Extensions Development (Chrome & Edge)レッスンでコードを書いて実行できますか?
はい。すべてのBrowser Extensions Development (Chrome & Edge)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- タブの管理と制御
- プログラムによるフォーム送信
- ユーザー操作の自動化
- Alarms APIによるタスクのスケジュール設定