Sensor: sự kiện reading

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.

Sự kiện reading được kích hoạt khi có dữ liệu đọc mới trên cảm biến.

Giao diện Sensor là lớp cơ sở, onreading và sự kiện reading chỉ có thể được sử dụng trên một trong các lớp dẫn xuất.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.

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

onreading = (event) => { }

Kiểu sự kiện

Một Event chung không có thuộc tính bổ sung.

Ví dụ

Đọc gia tốc

Ví dụ này thêm trình lắng nghe sự kiện để đọc các giá trị gia tốc của Accelerometer. Nó đọc sáu mươi lần mỗi giây.

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();

Thông số kỹ thuật

Specification
Generic Sensor API
# sensor-onreading

Tương thích trình duyệt

Xem thêm

  • Sự kiện activate của Sensor
  • Sự kiện error của Sensor