my.startAccelerometer

Use this JSAPI to start to receive the data from the accelerometer. The current mini program starts to receive the AccelerometerChange event.

Sample Code

copy
// .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

The incoming parameter is of the Object type with the following properties:

Property

Type

Required

Description

interval

String

No

The frequency of the callback function that listens to the acceleration data event.
Note: The frequency of the my.onAccelerometerChange JSAPI callback function is the same as the interval property.

Valid value:

  • game:applicable to the callback frequency for updating the game, approximately once every 20ms.
  • ui:applicable to the callback frequency for updating the UI, approximately once every 60ms.
  • normal:normal callback frequency, approximately once every 200ms.
    Note: This is the default value.

success

Function

No

The callback function that is called upon successful call.

fail

Function

No

The callback function that is called when the JSAPI call fails. When executed, it receives an err object.

complete

Function

No

The callback function that is called upon call completion (to be executed upon either successful or failed call).

err object

Parameter

Data type

Required

Description

error

Number

Yes

An error code that represents the specific error encountered.

errorMessage

String

Yes

An error message that describes the corresponding error code.