Use this API to listen for device shake events. This is useful for implementing “shake to refresh” or similar shake-triggered features.
Parameters
| Property | Type | Required | Description |
|---|
| success | Function | No | Callback when shake is detected |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
Code Example
Basic Usage
my.watchShake({
success() {
console.log('Device was shaken!');
}
});
Shake to Refresh
Page({
onLoad() {
this.enableShakeRefresh();
},
enableShakeRefresh() {
my.watchShake({
success: () => {
my.vibrate();
this.refreshData();
// Re-enable shake detection
this.enableShakeRefresh();
}
});
},
refreshData() {
my.showLoading({ content: 'Refreshing...' });
my.request({
url: 'https://api.example.com/data',
success: (res) => {
this.setData({ items: res.data });
my.hideLoading();
}
});
}
});
Shake for Random Selection
Page({
data: {
options: ['Option A', 'Option B', 'Option C', 'Option D'],
selected: null
},
onLoad() {
this.startShakeDetection();
},
startShakeDetection() {
my.watchShake({
success: () => {
my.vibrate();
this.selectRandom();
this.startShakeDetection();
}
});
},
selectRandom() {
const { options } = this.data;
const randomIndex = Math.floor(Math.random() * options.length);
this.setData({ selected: options[randomIndex] });
}
});
my.watchShake only triggers once per call. To continuously detect shakes, call it again in the success callback.
my.offWatchShake
Stop watching for shakes
my.vibrate
Trigger device vibration