创建一个新的alarm.
使用语法
browser.alarms.create(
name, // 可选的字符串(string)类型
alarmInfo // 可选的对象(object)类型
)
参数介绍
name
可选字符串(string)类型。
alarm的名称。默认为空的字符串。- alarm的名称可以在
alarms.get()
方法和alarms.clear()
方法中引用。同时它也可以通过alarms.onAlarm
监听方法传入的参数对象alarms.Alarm
的name属性访问到。 - Alarm的名称是唯一的 (在单个附件范围内). 如果传入了已经在这个附件存在的名称, 原来的同名alarm会被移除并且没有警告。
alarmInfo
可选-
对象(object)类型
. 你可以对过它来指定什么时间alarm会开始触发,其值可以是一个具体的时间值或者是一个延时(从alarm设置开始)。为了让alarm能复现,需要指定periodInMinutes。
在Chrome浏览器上,除非附件以非打包(unpackaged)方式加载,alarm的创建每分钟不允许超过一次。如果附件尝试设置
delayInMinutes
为小于1的值,alarm只能在到达1分钟之后才会触发,并且会变成每分钟触发一次。
alarmInfo对象
可以设置以下属性: -
when
可选double类型
. alarm第一次触发的时间,值为自1970-01-01 00:00:00 UTC过去的毫秒数。请使用Date.now()来获取
1970-01-01 00:00:00 UTC到当前时间过去的毫秒数。如果你设置了when属性,请不要设置delayInMinutes属性。
delayInMinutes
可选double类型
. alarm设置好到第一次触发之间的分钟数。如果你设置了delayInMinutes属性,请不要设置when属性。
periodInMinutes
可选double类型
. 如果设置此属性,alarm会从第一次触发开始每隔periodInMinutes分钟再次触发。如果你没有设置when及delayInMinutes属性,alarm会在alarm设置好之后periodInMinutes分钟第一次触发。如果periodInMinutes属性没有设置,则alarm只会触发一次。
浏览器兼容性
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
示例
Create a one-time delay-based alarm with "" for the name:
const delayInMinutes = 5;
browser.alarms.create({
delayInMinutes
});
Create a periodic delay-based alarm named "my-periodic-alarm":
const delayInMinutes = 5;
const periodInMinutes = 2;
browser.alarms.create("my-periodic-alarm", {
delayInMinutes,
periodInMinutes
});
Create a periodic absolute alarm named "my-periodic-alarm":
const when = 1545696000;
const periodInMinutes = 2;
browser.alarms.create("my-periodic-alarm", {
when,
periodInMinutes
});
This API is based on Chromium's chrome.alarms
API.
Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.