codecamp

Cordova 振动

此插件用于连接设备的振动功能。

步骤1 - 安装Vibration插件

我们可以通过运行以下代码在命令提示符窗口中安装此插件:

C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-vibration

步骤2 - 添加按钮

安装插件后,我们可以在 index.html 中添加按钮,稍后将用于触发振动。

<button id = "vibration">VIBRATION</button>
<button id = "vibrationPattern">PATTERN</button>

步骤3 - 添加事件监听器

现在我们将在 index.js 中的 onDeviceReady 内添加事件监听器。

document.getElementById("vibration").addEventListener("click", vibration);
document.getElementById("vibrationPattern").addEventListener("click", vibrationPattern);

步骤4 - 创建函数

这是插件非常容易使用。我们将创建两个函数。

function vibration() {
   var time = 3000;
   navigator.vibrate(time);
}

function vibrationPattern() {
   var pattern = [1000, 1000, 1000, 1000];
   navigator.vibrate(pattern);
}

第一个功能是使用时间参数。此参数用于设置振动的持续时间。一旦按下 VIBRATION 按钮,设备将振动三秒钟。

第二个函数是使用 pattern 参数。此阵列将要求设备振动一秒钟,然后等待一秒钟,然后重复该过程。

Cordova 闪屏
Cordova 白名单
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }