การจัดตารางงานด้วย API Alarms
เรียกใช้งานเบื้องหลังเป็นระยะและแบบหน่วงเวลาได้อย่างน่าเชื่อถือใน service worker ของ Manifest V3 โดยใช้ chrome.alarms
การจัดตารางงานด้วย API Alarms เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “การจัดตารางงานด้วย API Alarms” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดตารางงานด้วย API Alarms” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดตารางงานด้วย API Alarms”
เรียกใช้งานเบื้องหลังเป็นระยะและแบบหน่วงเวลาได้อย่างน่าเชื่อถือใน service worker ของ Manifest V3 โดยใช้ chrome.alarms คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดตารางงานด้วย API Alarms” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม
ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดการและควบคุมแท็บ
- การส่งแบบฟอร์มด้วยโปรแกรม
- การทำให้การโต้ตอบของผู้ใช้เป็นอัตโนมัติ
- การจัดตารางงานด้วย API Alarms