my.onAccelerometerChange
Use this JSAPI to listen to the data change event of the accelerometer. The triggering of acceleration data change events relies on the call to the my.startAccelerometer JSAPI.
The callback interval is 500ms. After the interface is called, the listening is automatically started. You can call the my.offAccelerometerChange JSAPI to stop listening.
Sample Code
// .js
Page({
listener(res) {
console.log("1" + JSON.stringify(res))
},
listener2(res) {
console.log("2" + JSON.stringify(res));
},
start() {
my.startAccelerometer({
success: () => {
my.onAccelerometerChange(this.listener);
my.onAccelerometerChange(this.listener2);
}
});
},
stop() {
my.stopAccelerometer();
},
offSingle() {
my.offAccelerometerChange(this.listener);
}
});
Parameters
Property | Type | Required | Description |
Function | Yes | The callback function to listen to the data change event of the accelerometer. |
listener
Callback Function
The incoming parameter is of the Object type with the following attributes. The following figure is used to better understand the data change rules of the accelerometer.
Property | Type | Description |
x | Number | Indicates the acceleration value in x-axis. Unit: m/s² For more, see the figure above. |
y | Number | Indicates the acceleration value in y-axis. Unit: m/s² For more, see the figure above. |
z | Number | Indicates the acceleration value in z-axis. Unit: m/s² For more, see the figure above. |
timestamp | Number | Indicates the timestamp. Unit: ns |