Skip to content

Accelerometer

startAccelerometer

TIP

The API usage is as follows: wx.startAccelerometer(Object object)

TIP

Depending on the performance of the model, the current CPU and memory usage, the interval setting and the actual wx.onAccelerometerChange The execution frequency of the callback function will be slightly different

  • Functional description: Start listening to acceleration data

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    intervalstringnormalYesThe execution frequency of the callback function for listening to acceleration data
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • The legal value of object.interval

    ValueDescription
    gameThe callback frequency suitable for updating the game is about 20ms/time
    uiThe callback frequency suitable for updating the UI is about 60ms/time
    normalThe normal callback frequency is about 200ms/time
  • Sample code:

js
wx.startAccelerometer({
  interval: 'game'
})

stopAccelerometer

TIP

The API usage is as follows: wx.stopAccelerometer(Object object)

  • Functional description: Stop listening for acceleration data

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.stopAccelerometer()

onAccelerometerChange

TIP

The API usage is as follows: wx.onAccelerometerChange(function listener)

  • Functional description: Listener for device orientation change events. Frequency based on wx.startAccelerometer() interval parameter. You can use wx.stopAccelerometer() Stops monitoring.

  • Parameters and descriptions: function callback, The callback function of the acceleration data event. The parameters Object.res are as follows:

    PropertiesTypeDescription
    xnumberX axis
    ynumberY axis
    znumberZ axis
  • Sample code:

js
wx.onAccelerometerChange(function (res) {
  console.log(res.x)
  console.log(res.y)
  console.log(res.z)
})

offAccelerometerChange

TIP

The API usage is as follows: wx.offAccelerometerChange(function listener)

  • Functional description: Remove the listener function of the acceleration data event

  • Parameters and descriptions: function callback, The listener function passed in by onAccelerometerChange. If this parameter is not passed, all listeners will be removed

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onAccelerometerChange(listener)
wx.offAccelerometerChange(listener) // Pass the same function object used for listening.