QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Web Alarms API Tutorial

Web Alarms API Tutorial

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); };
Jan 28, 2015Narayan Prusty
HTML5 Ambient Light APIPrevent Sleep using HTML5 Standby API
Comments: 3
  1. Sarika
    7 years ago

    HOW to add this web API in my project.

    ReplyCancel
  2. Sarika
    7 years ago

    Does it works for INTEL XDK hybrid app??

    ReplyCancel
    • Narayan Prusty
      7 years ago

      Yup it does work.

      ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

7 years ago 3 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • Page Visibility API Tutorial with Example
  • Web Notification API Tutorial with Example
  • HTML5 Battery Status API with Code Example
  • HTML5 Proximity API
  • JavaScript Limit Function Call Rate
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license