--- title: Accelerometer slug: Web/API/Accelerometer translation_of: Web/API/Accelerometer ---
The Accelerometer
interface of the Sensor APIs provides on each reading the acceleration applied to the device along all three axes.
To use this sensor, the user must grant permission to the 'accelerometer'
, device sensor through the Permissions
API.
If a feature policy blocks the use of a feature, it is because your code is inconsistent with the policies set on your server. This is not something that would ever be shown to a user. See {{httpheader('Feature-Policy')}} for implementation instructions.
Accelerometer
object.Acceleration is typically read in the {{domxref('Sensor.onreading')}} event callback. In the example below this occurs sixty times a second.
let accelerometer = new Accelerometer({frequency: 60}); accelerometer.addEventListener('reading', e => { console.log("Acceleration along the X-axis " + accelerometer.x); console.log("Acceleration along the Y-axis " + accelerometer.y); console.log("Acceleration along the Z-axis " + accelerometer.z); }); accelerometer.start();
Specification | Status | Comment |
---|---|---|
{{SpecName('Generic Sensor')}} | {{Spec2('Generic Sensor')}} | Defines sensors in general. |
{{SpecName('Accelerometer','#accelerometer-interface','Accelerometer')}} | {{Spec2('Accelerometer')}} |
{{Compat("api.Accelerometer")}}