All Products
Search
Document Center

Gyroscope

Last Updated: Feb 04, 2021

my.onGyroscopeChange(function callback)

Note: This interface is only supported in base library 1.9.0 or later versions and mPaaS V10.1.60 or later versions. For earlier versions, you need to perform compatibility processing. See Instructions on Mini Program base library.

This interface is used to listen to the gyroscope data change events. The interface starts listening to the events automatically once called. The callback interval is 500 ms. You can use my.offGyroscopeChange() to stop listening.

Parameters

Name Type Description
function callback Callback function for the events of gyroscope data changes.

Callback reutrn parameters

Name Type Description
x Number Angular velocity in X-axis direction
y Number Angular velocity in Y-axis direction
z Number Angular velocity in Z-axis direction

Sample code

  1. my.onGyroscopeChange((res)=>{
  2. console.log('gyroData.rotationRate.x = ' + res.x);
  3. console.log('gyroData.rotationRate.y = ' + res.y);
  4. console.log('gyroData.rotationRate.z = ' + res.z);
  5. });

my.offGyroscopeChange()

Note: This interface is only supported in base library 1.9.0 or later versions and mPaaS V10.1.60 or later versions. For earlier versions, you need to perform compatibility processing. See Instructions on Mini Program base library.

Rhis interface is used to stop listening to gyroscope data.

Sample code

  1. my.offGyroscopeChange();

Whether to pass callback value

  • If no callback value is passed, listeners on all event callback will be removed. See the following code example:

    1. my.offGyroscopeChange();
  • If the callback value is passed, only the corresponding callback events will be removed. See the following code example:

    1. my.offGyroscopeChange(this.callback);