> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rebellapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# my.onAccelerometerChange

> Listen for accelerometer data changes.

Use this API to listen to the data change event of the accelerometer. The triggering of acceleration data change events relies on the call to `my.startAccelerometer`.

## Parameters

| Property | Type     | Required | Description                                                                   |
| -------- | -------- | -------- | ----------------------------------------------------------------------------- |
| listener | Function | Yes      | The callback function to listen to the data change event of the accelerometer |

## Callback Parameters

| Property  | Type   | Description                              |
| --------- | ------ | ---------------------------------------- |
| x         | Number | Acceleration value in x-axis. Unit: m/s² |
| y         | Number | Acceleration value in y-axis. Unit: m/s² |
| z         | Number | Acceleration value in z-axis. Unit: m/s² |
| timestamp | Number | Timestamp. Unit: ns                      |

## Code Example

### Basic Usage

```javascript theme={null}
my.onAccelerometerChange((res) => {
  console.log('X:', res.x);
  console.log('Y:', res.y);
  console.log('Z:', res.z);
});
```

### Motion Detection

```javascript theme={null}
Page({
  data: {
    lastShake: 0
  },

  onLoad() {
    my.onAccelerometerChange((res) => {
      const acceleration = Math.sqrt(
        res.x * res.x + res.y * res.y + res.z * res.z
      );

      if (acceleration > 1.5) {
        const now = Date.now();
        if (now - this.data.lastShake > 1000) {
          this.setData({ lastShake: now });
          this.handleShake();
        }
      }
    });
  },

  handleShake() {
    my.vibrate();
    console.log('Device shaken!');
  }
});
```

### Tilt Detection for Games

```javascript theme={null}
Page({
  onLoad() {
    my.onAccelerometerChange((res) => {
      // Use x/y for tilt-based controls
      this.setData({
        tiltX: res.x * 90, // Convert to degrees
        tiltY: res.y * 90
      });
    });
  }
});
```

<Tip>
  Call `my.startAccelerometer` before using this API to begin receiving accelerometer data.
</Tip>

## Related APIs

<CardGroup cols={2}>
  <Card title="my.offAccelerometerChange" icon="xmark" href="/jsapi/device/accelerometer/my.offAccelerometerChange">
    Stop listening for changes
  </Card>

  <Card title="my.startAccelerometer" icon="play" href="/jsapi/device/accelerometer/my.startAccelerometer">
    Start accelerometer
  </Card>
</CardGroup>
