This specification defines an API to schedule a task at a specified time. When the indicated time is reached, the application that scheduled the task will be notified via a system message. If the application is not running when this happens, the system message will cause the application will be launched. Applications such as an alarm clock or an auto-updater may utilize this API to perform some action at a specified time.
This section is non-normative.
Example use of the ScheduledTask API for adding, getting and removing and listening for the alarm clock use cases:
How to add an alarm the ignores timezone information?
function onTaskAdded(task) {
console.log(task.data.message);
}
function onError(error) {
alert("Sorry, couldn't set the alarm: " + error);
}
var date = new Date("May 15, 2012 16:20:00");
navigator.taskScheduler.add(date, "ignoreTimezone", {
message: "Your soup is ready!"
}).done(onTaskAdded, onError);
How to add an alarm that respects timezone information?
function onTaskAdded(task) {
console.log(task.data.message);
}
function onError(error) {
alert("Sorry, couldn't set the alarm: " + error);
}
// Wake me up in 7 hours
var time = new Date(new Date(Date.now() + (3600000 * 7)));
navigator.taskScheduler.add(time, "respectTimezone", {
message: "Wake up!"
}).done(onTaskAdded, onError);
How to get all the scheduled tasks whose time is in the future?
navigator.taskScheduler.getPendingTasks().done(function(tasks) {
alert("There are " + tasks.length + " tasks set."));
}, function(error) {
alert("An error occurred getting the scheduled tasks.");
});
How to remove a scheduled task?
var request = navigator.taskScheduler.remove(id).done(function() {
alert("Task removed");
}, function(error) {
alert("Sorry, can't remove the task.");
});
How to register a listener for an task system message?
function onScheduledTaskFired(task) {
alert("task scheduled at: " + task.date);
}
navigator.setMessageHandler("task", onScheduledTaskFired);
How to know in advance if there exists any pending task system
message?
if (navigator.hasPendingMessages("task"))
alert("There is at least 1 pending 'task' system message.");
This specification defines conformance criteria for a single product: the user agent that implements the interfaces that it contains.
Implementations that use ECMAScript to implement the APIs defined in this specification MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [WEBIDL], as this specification uses that specification and terminology.
A JSON-serializable object is an object that when serialized or stringified conforms to the JSON Grammar as defined in
[ECMASCRIPT].
The EventHandler
interface represents a callback used for handling events as defined in [HTML5].
The Future interface provides asynchronous
access to the result of an operation that is ongoing, has yet to start, or has completed, as defined in
[DOM].
The concepts queue a task, event handler IDL attribute and fire a simple event are defined in [HTML5].
The terms event handler and event handler event types are defined in [HTML5].
The term system message is defined in [RUNTIME].
This section is non-normative.
The task scheduler supports the following features:
task system message is
sent to the application.
setTimeout() because it can actively wake the
system from sleeping.
Navigator
partial interface Navigator {
readonly attribute TaskScheduler taskScheduler;
}
The taskScheduler attribute provides the developer access to a
TaskScheduler.
TaskScheduler
The TaskScheduler interface exposes methods to get, set or remove scheduled tasks. ScheduledTasks are
application specific, so there is no way to see the tasks scheduled by other applications nor to modify them.
Developers should set a message handler for the task system
message to listen for when scheduled tasks should be executed.
enum TimezoneDirective { "respectTimezone", "ignoreTimezone" };
interface TaskScheduler {
Future getPendingTasks();
Future add(Date date, TimezoneDirective timezoneDirective, optional any data);
Future remove(DOMString taskId);
};
When invoked, the getPendingTasks() method must run the following steps:
Future object and resolver its associated resolver.
DOMError object whose name is "UnknownError".
reject(value) method with error as value argument.
ScheduledTask objects that were retrieved.
accept(value) method with tasks as value argument.
When invoked, the add(date, timezoneDirective[,
data]) method must run the following steps:
date. Depending on the timezoneDirective argument, the system will honor the timezone
information of that [ECMASCRIPT] Date object or not. The system must associate the JSON-serializable data with the task if provided.
Future object and resolver its associated resolver.
DOMError object whose name is "InvalidStateError" if the
date argument is in the past, "QuotaExceededError" if the data argument exceeds
an implementation-dependent size limit, and "UnknownError" otherwise.
reject(value) method with error as value
argument.
ScheduledTask object.
id attribute to the unique identifier returned by the system for the newly
registered task.
date attribute to the date argument.
timezoneDirective attribute to the timezoneDirective
argument.
data attribute to the data argument, if provided.
accept(value) method with task as value
argument.
When invoked, the remove(taskId) method
must run the following steps:
taskId identifier.
Future object and resolver its associated resolver.
DOMError object whose name is "UnknownError".
reject(value) method with error as value
argument.
true if the task was removed, and to false if there was no task
with the given identifier.
accept(value) method with removed as value
argument.
ScheduledTask
The ScheduledTask interface captures the properties of a scheduled task.
interface ScheduledTask {
readonly attribute DOMString id;
readonly attribute Date date;
readonly attribute TimezoneDirective timezoneDirective;
readonly attribute any data;
};
The id attribute returns an identifier for the given
ScheduledTask object that is unique within the origin. An implementation must maintain
this identifier when a ScheduledTask is added.
The date attribute is a Date object representing when
the task is scheduled to run.
The timezoneDirective attribute indicates if the
timezone information of the Date object is ignored.
The data attribute optionally represents the JSON-serializable
data associated with the task.
task system message
An task system message is fired when a scheduled task should be
executed. This event is originated from the system and will wake up the application if it is not currently running.
The developer should set message handler for the task system message to listen
for when scheduled tasks should be executed. The ScheduledTask that was went off will be passed in
argument to the system message callback.
This section is non-normative.
The implementation needs to consider timezones and respect daylight savings. We will provide examples in this section to clarify how non-obvious cases should be handled.
This section is non-normative.
Let's consider an alarm on March 10, 2013 at 2:00am in a timezone where daylight savings time starts at that time (e.g. in San Francisco, CA):
var time = new Date(2013, 2, 10, 2, 00, 00); navigator.taskScheduler.add(time, "ignoreTimezone");
In San Francisco, CA, clocks are turned forward 1 hour on Sunday, March 10, 2013 at 2:00am due to daylight savings. In this case, the alarm needs to fire at 3:00am, i.e. one minute after 1:59am.
Let's consider an alarm on November 3, 2013 at 1:10am in a timezone where daylight savings time ends on that date (e.g. in San Francisco, CA):
var time = new Date(2013, 10, 3, 1, 10, 00); navigator.taskScheduler.add(time, "ignoreTimezone");
In San Francisco, CA, clocks are turned backward 1 hour on Sunday, November 3, 2013 at 2:00am. In this case, the alarm fires once the first time that the device is running and the local time is past 1:10am on November 3, 2013. After that the alarm is deleted.
This section is non-normative.
Let's consider an alarm set for January 21, 2013 at 7:00am in Pacific Standard Time (PST) with timezoneDirective argument set to "ignoreTimezone":
// User is currently in Pacific Standard Time (PST) var time = new Date(2013, 0, 21, 7, 0, 00); navigator.taskScheduler.add(time, "ignoreTimezone");
If the user is traveling to New York on that day and currently in Eastern Standard Time (EST), the alarm needs to fire on January 21, 2013 when it is 7:00am in Eastern Standard Time (EST).
Let's consider an alarm set for January 21, 2013 at 7:00am in Pacific Standard Time (PST) with timezoneDirective argument set to "respectTimezone":
// User is currently in Pacific Standard Time (PST) var time = new Date(2013, 0, 21, 7, 0, 00); navigator.taskScheduler.add(time, "respectTimezone");
If the user is traveling to New York on that day and currently in Eastern Standard Time (EST), the alarm needs to fire on January 21, 2013 when it is 7:00am in Pacific Standard Time (PST), that is 10:00am for the user in Eastern Standard Time (EST).
We would like to thank Kan-Ru Chen, Mounir Lamouri, Gene Lian and Jonas Sicking for their work on the API design, as well as the WebAPI/B2G teams at Mozilla [B2G-ALARM].