Through the chrome.alarms.* API, the Chrome browser Extender can make plans to execute code periodically, or execute code at a specified time.
To use the chrome.alarms.* API, you first need to declare the alarms authorization in the Manifest.json file as follows:
{
"Permissions": [
"Alarms"
],
}
The properties of the Chrome.alarms.Alarm object are as follows:
Optional
Property name |
Type |
Required/ optional |
Comment |
name |
string |
Required |
Alarm name |
Scheduledtime |
Double |
Required |
Time to trigger alarm, Unit MS |
Double |
Non-null indicates the interval of alarm periodic execution, unit minute |
Common methods in the Chrome.alarms API:
· Create a alarm
Chrome.alarms.create (string name, Object Alarminfo)
This is a synchronous method, the Name property is optional, and is empty for "". The properties of the Alarminfo object are as follows:
Property name |
Type |
Required/ optional |
comments |
when |
Double |
Optional |
Time to trigger alarm, Unit MS |
Delayinminutes |
Double |
Optional |
Onalarm Event issue delay time, Unit minute |
Periodinminutes |
Double |
Optional |
Non-null indicates the interval of alarm periodic execution, in units minute |
Creates a new alarm at the time specified by Alarminfo and issues the Onalarm event. If there are already duplicate alarm, replace the existing alarm.
To reduce the effect of creating alarm on operational efficiency, the Chrome browser restricts up to one alarm in a minute, and any alarm that might break that limit will be postponed any time. Of course, there is no such limit during debugging.
· Gets the alarm of the specified name
Chrome.alarms.get (string name, function (Alarm Alarm) {...})
· Get all Alarm
Chrome.alarms.getAll (function (array of Alarm alarms) {...})
· Delete Alarm by name
Chrome.alarms.clear (string name, function (Boolean wascleared) {...})
· Clear all Alarm
Chrome.alarms.clearAll (Function (Boolean wascleared) {...})
· Listen for events that occur in alarm for the event page
Chrome.alarms.onAlarm.addListener (function (Alarm Alarm) {...})
The alarm in the callback function is the alarm object that triggered the event.
Chrome Browser Extension Development Series IX: Chrome browser's chrome.alarms.* API