Nhảy tới nội dung

my.watchShake

watchShake

my.watchShake là API dùng để nhận sự kiện lắc thiết bị. Success callback chỉ được gọi một lần khi thiết bị được lắc sau khi API đã được gọi. Cần gọi lại API này nếu muốn nhận sự kiện thiết bị lắc ở lần sau.

Khả dụng: Hỗ trợ từ version 1.86.3 trở lên.

Quét mã để trải nghiệm

Xem code mẫu trên Tini Studio

API Params

Thuộc tínhKiểu dữ liệuMô tả
successFunctionCallback function khi thiết bị được lắc.
failFunctionCallback function khi có lỗi xảy ra.
completeFunctionCallback function khi thực hiện hoàn tất.

Sample Code

<view>
<block-header title="Usage" description="Receive a single callback when device is shaked." />
<view class="block-content">
<button class="button-full" onTap="onWatchShake">Watch shake</button>
</view>
</view>
Page({
data: {
isWatchingShake: false
},
onWatchShake() {
if (!this.data.isWatchingShake) {
my.watchShake({
success: () => {
this.setData({ isWatchingShake: false });
my.alert({
title: 'Success',
content: 'Device is shaked'
});
},
fail: (e) => {
this.setData({ isWatchingShake: false });
my.alert({ title: 'Fail', content: JSON.stringify(e) });
}
});

this.setData({ isWatchingShake: true });
}
}
});