There are times when vibration is needed into the app, such as when back button is pressed, on display of any alert or warning message, etc. This plugin will help you to connect to device's vibration functionality.
Step 1 - Install Vibration Plugin
Open the command prompt window and run the following code to install this plugin:
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-vibration
When the plugin is installed you can add buttons in index.html file that will be used later to execute the vibration.
<button id = "vibrationmode">VIBRATION</button>
<button id = "Patternofvibration">PATTERN</button>
Now, in index.js file, inside onDeviceReady, you can add event listeners.
document.getElementById("vibrationmode").addEventListener("click", vibrationmode);
document.getElementById("Patternofvibration").addEventListener("click", Patternofvibration);
Step 4 - Create Functions
This plugin can be very easily used. You have to create two functions.
function vibrationmode() {
var time = 3000;
navigator.vibrate(time);
}
function Patternofvibration () {
var pattern = [1000, 1000, 1000, 1000];
navigator.vibrate(pattern);
}
The first function is taking time parameter that is used for setting the duration of the vibration. When we press the VIBRATION button the device will vibrate for three seconds.
The second function have pattern parameter. This array will tell the device to have break of one second between the consecutive vibrates.
Also Read: Cordova- Whitelist
Leave Comment