This plugin is used for connecting to device's vibration functionality.
Step 1 - Installing Vibration Plugin
We can install this plugin in command prompt window by running the following code −
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-vibration
Step 2 - Add Buttons
Once the plugin is installed we can add buttons in index.html that will be used later to trigger the vibration.
<button id = "vibration">VIBRATION</button> <button id = "vibrationPattern">PATTERN</button>
Step 3 - Add Event Listeners
Now we are going to add event listeners inside onDeviceReady in index.js.
document.getElementById("vibration").addEventListener("click", vibration); document.getElementById("vibrationPattern").addEventListener("click", vibrationPattern);
Step 4 - Create Functions
This plugin is very easy to use. We will create two functions.
function vibration() { var time = 3000; navigator.vibrate(time); } function vibrationPattern() { var pattern = [1000, 1000, 1000, 1000]; navigator.vibrate(pattern); }
The first function is taking time parameter. This parameter is used for setting the duration of the vibration. Device will vibrate for three seconds once we press VIBRATION button.
The second function is using pattern parameter. This array will ask device to vibrate for one second, then wait for one second, then repeat the process again.
Tidak ada komentar:
Posting Komentar