这篇翻译不完整。请帮忙从英语翻译这篇文章。
创建一个新的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只会触发一次。
浏览器兼容性
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.
Desktop | Mobile | ||||
---|---|---|---|---|---|
Basic support | Chrome Full support Yes | Edge No support No | Firefox Full support 45 | Opera Full support Yes | Firefox Android Full support 48 |
Legend
- Full support
- Full support
- No support
- No support
示例
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.
// Copyright 2015 The Chromium Authors. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.