جدولة المهام باستخدام Alarms API
شغّل أعمالًا خلفية دورية ومؤجلة بصورة موثوقة في عامل خدمة وفق Manifest V3 باستخدام chrome.alarms.
جدولة المهام باستخدام Alarms API درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
الأسئلة الشائعة
هل درس «جدولة المهام باستخدام Alarms API» مجاني؟
نعم — نص درس «جدولة المهام باستخدام Alarms API» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
ماذا ستتعلم في «جدولة المهام باستخدام Alarms API»؟
شغّل أعمالًا خلفية دورية ومؤجلة بصورة موثوقة في عامل خدمة وفق Manifest V3 باستخدام chrome.alarms. تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Browser Extensions Development (Chrome & Edge)؟
لا تُشترط خبرة سابقة. Browser Extensions Development (Chrome & Edge) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «جدولة المهام باستخدام Alarms API»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Browser Extensions Development (Chrome & Edge) هذا؟
نعم. كل درس في Browser Extensions Development (Chrome & Edge) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إدارة علامات التبويب والتحكم فيها
- إرسال النماذج برمجيًا
- أتمتة تفاعلات المستخدم
- جدولة المهام باستخدام Alarms API