

English
Monitoring acceleration data, frequency: 5 times per second, the monitoring will start automatically after the interface is called, you can use uni.offAccelerometer
to cancel the monitoring.
元服务 | 小红书小程序 |
---|---|
1.0.4+ | x |
HarmonyOS |
---|
x |
parameter
function listener
Listener function for acceleration data events
parameter
Object res
Parameter | Type | Description |
---|---|---|
x | Number | X-axYes |
y | Number | Y-axYes |
z | Number | Z axYes |
Tips
example
uni.onAccelerometerChange(function (res) {
console.log(res.x);
console.log(res.y);
console.log(res.z);
});
Cancel listening to acceleration data.
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 元服务 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|---|
HBuilderX 2.9.6+ | HBuilderX 2.9.6+ | 基础库 2.9.3+ | x | x | x | x | √ | √ | 1.0.4+ | x |
HarmonyOS |
---|
x |
parameter
function listener
The listener function passed in by onAccelerometerChange. If this parameter is not passed, all listener functions will be removed.
example
const listener = function (res) { console.log(res) }
uni.onAccelerometerChange(listener)
uni.offAccelerometerChange(listener) // 需传入与监听时同一个的函数对象
Start monitoring acceleration data.
Platform Difference Description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 元服务 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|---|
√ | √ | √ | x | √ | √ | √ | √ | √ | 1.0.4+ | x |
HarmonyOS |
---|
x |
OBJECT parameter description
Parameter Name | Type | Default | Required | Description | Platform Difference Description |
---|---|---|---|---|---|
interval | String | normal | No | Monitor the execution frequency of the acceleration data callback function | WeChat MiniApp, Baidu MiniApp, QQ MiniApp, Kuaishou MiniApp, JD MiniApp |
success | Function | No | Callback for successful interface call | ||
fail | Function | No | Callback function for interface call failure | ||
complete | Function | No | The callback function of the end of the interface call (it will be executed when the call succeeds or fails) |
Legal values for interval
value | description |
---|---|
game | Callback frequency suitable for updating games, around 20ms/time |
ui | Callback frequency suitable for updating UI, around 60ms/time |
normal | Normal callback frequency, about 200ms/time |
example
uni.startAccelerometer();
Stop listening to acceleration data.
元服务 | 小红书小程序 |
---|---|
1.0.4+ | x |
HarmonyOS |
---|
x |
OBJECT parameter description
Parameter Name | Type | Required | Description |
---|---|---|---|
success | Function | No | Callback for successful interface call |
fail | Function | No | Callback function for interface call failure |
complete | Function | No | The callback function for the end of the interface call (it will be executed when the call succeeds or fails) |
example
uni.stopAccelerometer();