This document includes material copied from the W3C Editor's Draft of the Task Scheduler specification from 13 October 2014.
Copyright © 2015 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
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 functional event on a service worker. A task event will be delivered to a service worker, regardless of whether the application is active on user agent. Applications such as an alarm clock or an auto-updater may utilize this API to perform certain action at a specified time.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This specification is being republished as a Working Group Note as an indication that it not being progressed further as a Recommendation track document.
This document was published by the System Applications Working Group as a Working Group Note. If you wish to make comments regarding this document, please send them to public-sysapps@w3.org (subscribe, archives). All comments are welcome.
Publication as a Working Group Note does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 1 August 2014 W3C Process Document.
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 set an alarm 10 minutes from now?
// https://example.com/serviceworker.js this.ontask = function(task) { alert(task.data.message); console.log("Task scheduled at: " + new Date(task.time)); // From here on we can write the data to IndexedDB, send it to any open windows, // display a notification, etc. } // https://example.com/webapp.js function onTaskAdded(task) { console.log("Task successfully scheduled."); } function onError(error) { alert("Sorry, couldn't set the alarm: " + error); } navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { serviceWorkerRegistration.taskScheduler.add(Date.now() + (10 * 60000), { message: "It's been 10 minutes, your soup is ready!" }).then(onTaskAdded, onError); });
How to get all the scheduled tasks whose time is in the future?
navigator.serviceWorker.getRegistration().then(function(registration) { registration.taskScheduler.getPendingTasks().then(function(tasks) { alert("There are " + tasks.length + " tasks set."); }, function(error) { alert("An error occurred getting the scheduled tasks."); }); }, function(error) { alert("An error occurred getting the scheduled tasks."); });
How to remove a scheduled task?
navigator.serviceWorker.getRegistration().then(function(registration) { var request = registration.taskScheduler.remove(id).then(function() { alert("Task removed"); }, function(error) { alert("Sorry, can't remove the task."); }); }, function(error) { alert("An error occurred getting the scheduled tasks."); });
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 Promise
interface provides
asynchronous access to the result of an operation that is ongoing, has yet to start, or has completed, as defined in
[ECMASCRIPT6].
The concepts queue a task, event handler IDL attribute and fire a simple event are defined in [HTML5].
The concepts event and fire an event named eare defined in [DOM].
The terms event handler and event handler event types are defined in [HTML5].
Service worker, service worker registration, ServiceWorker, ServiceWorkerRegistration, ServiceWorkerGlobalScope, ExtendableEvent, and Handle Functional Event are defined in [SERVICE-WORKERS].
Below is a summary of requirements associated with this API:
This section is non-normative.
The task scheduler supports the following features:
ScheduledTask
has a unique identifier that can be used to specify and remove the scheduled task.
task
event is
sent to the application.
setTimeout()
because it can actively wake the
system from sleeping and scheduled task are not lost when closing the application or restarting the system.
ServiceWorkerRegistration
The Service Worker specification defines a ServiceWorkerRegistration
interface
[SERVICE-WORKERS], which this specification extends.
partial interface ServiceWorkerRegistration { 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 an ontask event handler in the associated service worker to listen for the task
event when scheduled tasks should be executed.
interface TaskScheduler { Promise getPendingTasks(); Promise add(DOMTimeStamp time, optional any data); Promise remove(DOMString taskId); };
When invoked, the getPendingTasks()
method must run the following steps:
Promise
object and resolver its associated resolver.
DOMException
exception whose name
is the same as the error
returned.
value
.
ScheduledTask
objects that were retrieved.
fulfill
algorithm with tasks as value
.
When invoked, the add(time[, data])
method
must run the following steps:
time
(number of milliseconds since the epoch). If the time
argument is in the past, the task
will be executed as soon as possible, asynchronously. The system must associate the
JSON-serializable data
with the task if provided.
Promise
object and resolver its associated resolver.
DOMException
exception whose name
is
"QuotaExceededError"
if the data
argument exceeds an implementation-dependent size limit, or
whose name
is the same as the error returned otherwise.
value
.
ScheduledTask
object.
id
attribute to the unique identifier returned by the system for the newly
registered task.
time
attribute to the time
argument.
data
attribute to the data
argument, if provided.
fulfill
algorithm with task as value
.
When invoked, the remove(taskId)
method
must run the following steps:
taskId
identifier.
Promise
object and resolver its associated resolver.
DOMException
exception whose name
is the same as the error
returned.
value
.
true
if the task was removed, and to false
if there was no task
with the given identifier.
fulfill
algorithm with removed as
value
.
ScheduledTask
The ScheduledTask
interface captures the properties of a scheduled task.
interface ScheduledTask {
readonly attribute DOMString id;
readonly attribute DOMTimeStamp time;
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 time
attribute is the time at which this task is scheduled to
fire, in milliseconds past the epoch (e.g. Date.now() + n). Due to performance, the task may be delayed past this time.
The data
attribute optionally represents the JSON-serializable
data associated with the task.
The Service Worker specification defines a ServiceWorkerGlobalScope
interface
[SERVICE-WORKERS], which this specification extends.
partial interface ServiceWorkerGlobalScope {
attribute EventHandler ontask;
};
ServiceWorkerGlobalScope
object.
event handler | event handler event type |
---|---|
ontask |
task |
TaskEvent
Interface
The TaskEvent
interface represents a scheduled task.
interface TaskEvent : ExtendableEvent { readonly attribute ScheduledTask task; };
task
event to service worker
task
event is fired when a scheduled task should be
executed. The scheduled task is originated from the system and will wake up a service worker if it is not currently running.
When the scheduled task task went off by the system, the user agent must (unless otherwise specified) run these steps:
task
given task on global.To fire a service worker task event named e given task, fire an event named e with an event using the TaskEvent
interface whose task
attribute is initialized to a new ScheduledEvent
object representing task.
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].