W3cubDocs

/Web APIs

Sensor

The Sensor interface of the Sensor APIs is the base class for all the other sensor interfaces. This interface cannot be used directly. Instead it provides properties, event handlers, and methods accessed by interfaces that inherit from it.

This feature may be blocked by a Permissions Policy set on your server.

EventTarget Sensor

When initially created, the Sensor object is idle, meaning it does not take measures. Once the start() method is called, it prepares itself to read data and, once ready, the activate event is sent and the sensor becomes activated. It then sends a reading event each time new data is available.

In case of an error, the error event is sent, reading stops, and the Sensor object becomes idle again. The start() method needs to be called again before it can read further data.

Interfaces based on Sensor

Instance properties

Sensor.activated Read only

Returns a boolean value indicating whether the sensor is active.

Sensor.hasReading Read only

Returns a boolean value indicating whether the sensor has a reading.

Sensor.timestamp Read only

Returns the timestamp of the latest sensor reading.

Instance methods

Sensor.start()

Activates one of the sensors based on Sensor.

Sensor.stop()

Deactivates one of the sensors based on Sensor.

Events

activate

Fired when a sensor becomes activated.

error

Fired when an exception occurs on a sensor.

reading

Fired when a new reading is available on a sensor.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Sensor 67 79 No No 54 No 67 67 No 48 No 9.0
activate_event 67 79 No No 54 No 67 67 No 48 No 9.0
activated 67 79 No No 54 No 67 67 No 48 No 9.0
error_event 67 79 No No 54 No 67 67 No 48 No 9.0
hasReading 67 79 No No 54 No 67 67 No 48 No 9.0
reading_event 67 79 No No 54 No 67 67 No 48 No 9.0
start 67 79 No No 54 No 67 67 No 48 No 9.0
stop 67 79 No No 54 No 67 67 No 48 No 9.0
timestamp 67 79 No No 54 No 67 67 No 48 No 9.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Sensor