UNPKG

1.02 kBJavaScriptView Raw
1import { MethodHandler } from '../../utils/handler';
2const vibrator = function vibrator(mm) {
3 try {
4 return window.navigator.vibrate(mm);
5 }
6 catch (e) {
7 console.warn('当前浏览器不支持 vibrate。');
8 }
9};
10/**
11 * 使手机发生较短时间的振动(15 ms)。仅在 iPhone 7 / 7 Plus 以上及 Android 机型生效
12 */
13export const vibrateShort = ({ success, fail, complete } = {}) => {
14 const handle = new MethodHandler({ name: 'vibrateShort', success, fail, complete });
15 if (vibrator(15)) {
16 return handle.success();
17 }
18 else {
19 return handle.fail({ errMsg: 'style is not support' });
20 }
21};
22/**
23 * 使手机发生较长时间的振动(400 ms)
24 */
25export const vibrateLong = ({ success, fail, complete } = {}) => {
26 const handle = new MethodHandler({ name: 'vibrateLong', success, fail, complete });
27 if (vibrator(400)) {
28 return handle.success();
29 }
30 else {
31 return handle.fail({ errMsg: 'style is not support' });
32 }
33};