Learning how to create and delete alarms is a basic alarm management skill:
To create an absolute alarm, create an instance of the AlarmAbsolute:Alarm interface.
You must define the time and date when the alarm is triggered as a Date object.
/* Alarm is triggered at 8:00 on April 4, 2012 */ var date = new Date(2012, 3, 4, 8, 0); var alarm1 = new webapis.AlarmAbsolute(date);
To create a relative alarm, create an instance of the AlarmRelative:Alarm interface.
You must define the delay after which the alarm is triggered.
/* Alarm is triggered in 3 hours */ var alarm2 = new webapis.AlarmRelative(3 * webapis.alarm.PERIOD_HOUR);
To create a recurring absolute alarm, create an instance of the AlarmAbsolute interface.
You must define the time and date when the alarm is triggered as a Date object. In addition, you can define a time period or the weekdays when the alarm is repeated.
/* Alarm is triggered for the first time at 8:00 on April 4, 2012 */ var date = new Date(2012, 3, 4, 8, 0); /* Alarm repeats every 2 days, at 08:00, starting after April 4, 2012 */ var alarm3 = new webapis.AlarmAbsolute(date, 2 * webapis.alarm.PERIOD_DAY); /* Alarm repeats every Saturday and Sunday, at 08:00, starting after April 4, 2012 */ var alarm4 = new webapis.AlarmAbsolute(date, ["SA", "SU"]);
Note You cannot define both a time period and a weekday recurrence for the same alarm. To create a recurring relative alarm, create an instance of the AlarmRelative interface.
You must define the delay after which the alarm is triggered, and the period after which it is repeated.
/* Alarm is triggered for the first time in 1 hour, and then repeated every 2 minutes */ var alarm5 = new webapis.AlarmRelative(webapis.alarm.PERIOD_HOUR, 2 * webapis.alarm.PERIOD_MINUTE);
To obtain a list of all the alarms that have been set on a device but not yet triggered, use the getAll() method:
var alarms = webapis.alarm.getAll(); console.log(alarms.length + " alarms present in the storage.");
To delete an alarm, use the remove() method with the alarm ID:
webapis.alarm.remove(alarm1.id);
To delete all alarms at once, use the removeAll() method.