Sensor: reading event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The reading event is fired when a new reading is available on a sensor.

The Sensor interface is a base class, onreading and the reading event may only be used on one of the derived classes.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("reading", (event) => {});

onreading = (event) => {};

Event type

A generic Event with no added properties.

Examples

Reading acceleration

This example adds an event listener to read acceleration values of an Accelerometer. It reads sixty times a second.

js
const acl = new Accelerometer({ frequency: 60 });
acl.addEventListener("reading", () => {
  console.log(`Acceleration along the X-axis ${acl.x}`);
  console.log(`Acceleration along the Y-axis ${acl.y}`);
  console.log(`Acceleration along the Z-axis ${acl.z}`);
});
acl.start();

Specifications

Specification
Generic Sensor API
# sensor-onreading

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
reading event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also