# uni.onAccelerometerChange(CALLBACK)

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.

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

  • The H5 terminal needs to be deployed on the https service to obtain the acceleration information, and the local preview (localhost) can still use the http protocol.

example

uni.onAccelerometerChange(function (res) {
	console.log(res.x);
	console.log(res.y);
	console.log(res.z);
});

# uni.offAccelerometerChange(CALLBACK)

Cancel listening to acceleration data.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
HBuilderX 2.9.6+ HBuilderX 2.9.6+ Basic library 2.9.3+ x x x 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) // 需传入与监听时同一个的函数对象

# uni.startAccelerometer(OBJECT)

Start monitoring acceleration data.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
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();

# uni.stopAccelerometer(OBJECT)

Stop listening to acceleration data.

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