This post is a part 7 of Web Alarms API Tutorial post series.
This API is supported by both mobile and desktop devices. This API allows us to schedule a callback. This API uses the device’s alarm settings.
navigator.alarms property is the heart of the Web Alarms API.
Let’s have a look at the API:
var alarmId;
//add() method registers a callback notification at the given date and time
//second parameter indicates weather the time should be relative to the time zone. More on it at http://www.w3.org/TR/web-alarms/#timezone-dst
//third parameter is a object with some data that will be returned when the callback is fired.
var request = navigator.alarms.add(
new Date("July 12, 2014 01:30:23"),
"respectTimezone",
{somedata: "data"}
);
//alarm notification registered successfully.
request.onsuccess = function (e) {
alarmId = e.target.result;
};
//an error occurred during registration.
request.onerror = function (e) {
alert(e.target.error.name);
};
//used to remove an alarm.
var request = navigator.alarms.remove(alarmId);
//removed successfully
request.onsuccess = function (e) {
alert("alarm removed");
};
//error occurred while removing
request.onerror = function (e) {
alert(e.target.error.name);
};
//is fired when the registered notfication occures.
navigator.alarms.addEventListener("alarm", function (e) { alert("alarm fired: " + JSON.stringify(e)); }, false);
//retrieve all registered alarms
var request = navigator.alarms.getAll();
request.onsuccess = function (e) { alert(JSON.stringify(e.target.result)); };
request.onerror = function (e) { alert(e.target.error.name); };
//add() method registers a callback notification at the given date and time
//second parameter indicates weather the time should be relative to the time zone. More on it at http://www.w3.org/TR/web-alarms/#timezone-dst
//third parameter is a object with some data that will be returned when the callback is fired.
var request = navigator.alarms.add(
new Date("July 12, 2014 01:30:23"),
"respectTimezone",
{somedata: "data"}
);
//alarm notification registered successfully.
request.onsuccess = function (e) {
alarmId = e.target.result;
};
//an error occurred during registration.
request.onerror = function (e) {
alert(e.target.error.name);
};
//used to remove an alarm.
var request = navigator.alarms.remove(alarmId);
//removed successfully
request.onsuccess = function (e) {
alert("alarm removed");
};
//error occurred while removing
request.onerror = function (e) {
alert(e.target.error.name);
};
//is fired when the registered notfication occures.
navigator.alarms.addEventListener("alarm", function (e) { alert("alarm fired: " + JSON.stringify(e)); }, false);
//retrieve all registered alarms
var request = navigator.alarms.getAll();
request.onsuccess = function (e) { alert(JSON.stringify(e.target.result)); };
request.onerror = function (e) { alert(e.target.error.name); };