Sensor API
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 EventTarget/addEventListener:
addEventListener("reading", (event) => { })
Alternatively, assign to the event handler property of the interface:
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.
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
Browser compatibility
See also
- Sensor Sensor.activate_event event
- Sensor Sensor.error_event event
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
