大网数据广告活动
Web 标准与前端开发

Sensor

· 52CE 我爱测

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.

Sensor API

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.

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

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

Interfaces based on Sensor

Below is a list of interfaces based on the Sensor interface.

  • Accelerometer
  • AmbientLightSensor
  • GravitySensor
  • Gyroscope
  • LinearAccelerationSensor
  • Magnetometer
  • OrientationSensor

Instance properties

  • Sensor.activated
    • : Returns a boolean value indicating whether the sensor is active.
  • Sensor.hasReading
    • : Returns a boolean value indicating whether the sensor has a reading.
  • Sensor.timestamp
    • : 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

  • Sensor.activate_event
    • : Fired when a sensor becomes activated.
  • Sensor.error_event
    • : Fired when an exception occurs on a sensor.
  • Sensor.reading_event
    • : Fired when a new reading is available on a sensor.

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。