User experience is really important. One of the parts of good user experience is using all of the device capabilities to return the information to the user. One of them in the vibration. Imagine users input their username and password in your app and in case the failure, you vibrate the phone to notify them they made a mistake. Also, vibration is an important part of games as it dramatically improves the gaming experience.
Read 20 Best Resources to Prepare PhoneGap Interviews
Controlling of the vibration can be easily integrated into Phonegap. We must use org.apache.cordova.vibration plugin. This plugin aligns with the W3C vibration specification http://www.w3.org/TR/vibration/.
Installation
You can install the plugin with Cordova CLI issuing the following command.
cordova plugin add org.apache.cordova.vibration
Suggested Read: 20 Best PhoneGap Interview Questions
Usage
This plugin defines global objects including navigator.vibrate. We can use this to call different vibration methods and control how the vibration occurs. For that, we use method vibrate.
Time is in milliseconds. So to vibrate 4 seconds, we need to write the following code.
navigator.vibrate(4000)
There are some limitations and problems for certain platforms. One of them is iOS where vibrate ignores the time and vibrates for a pre-set amount of time. This is probably part of Apple’s rule of keeping the user experience the same on all devices and in all apps. Another problem is with Windows Phone and Blackberry where time is limited to 5 seconds. So if we for example set to 8 seconds (or 8000 milliseconds), it will only vibrate for maximum of 5 seconds and then stop.
Vibrate with a pattern
One of the cool features is that we can control the pattern of vibration. This is really useful inside of the games when we want to create gaming experience by including vibration on some hard, difficult, scary moments and just increase the tempo to make if more dramatic. Unfortunately, this is only supported on Android and Windows.
navigator.vibrate(pattern);
The pattern is a sequence of durations (in milliseconds) for which to turn on or off the vibrator. (Array of Numbers). So if we want to vibrate 3 seconds, wait for 2 and then again vibrate for 2 seconds, we can use the following code.
// Vibrate for 3 seconds
// Wait for 2 seconds
// Vibrate for 2 seconds
navigator.vibrate([3000, 2000, 2000]);
The duration of the pattern can be long as you want.
Cancel vibration
Sometimes we want to cancel the vibration. To do this, we need to call vibrate method again with time set to 0. This will overwrite the current vibration and because it’s set to 0, it will immediately stop.
Conclusion
Using the phone vibration capability inside of the Phonegap app can be done very easily. All we have to do is to include the plugin and use the vibrate method. Be careful when you use it because extensive vibration can make users unhappy. At the same time, be aware of different limitations on supported platforms.
Thanks for reading. Please read Interview Questions on PhoneGap if you are looking for PhoneGap Interview Questions.
Originally published at https://www.tumblr.com.