The new APIs available in modern browsers tend to be more mobile apps than traditional desktop apps, such as JavaScript geographic information APIs. Another JavaScript API for mobile apps is the vibration (vibration) API. Obviously, the API is to allow mobile programmers to use JavaScript to invoke the vibration function of the phone, and to set the mode and length of vibration.
Determine the browser's support for the vibration API
A good habit is to check your current application environment and whether the browser supports the vibration API before using it. Here's how to detect:
Copy Code code as follows:
Standards ftw!
var supportsvibrate = "vibrate" in navigator;
In the Window.navigator object there is only one api:vibrate of vibration.
Basic Application of Vibration API
This navigator.vibrate function can accept a numeric parameter, or it can accept a numeric array, and when using array parameters, the number of odd digits is the vibrational seconds, and even digits are the number of seconds to wait.
Vibration 1 seconds
Copy Code code as follows:
Navigator.vibrate (1000);
Vibration multiple times
The parameters are vibration 3 seconds, wait 2 seconds, and then vibrate for 1 seconds.
Copy Code code as follows:
Navigator.vibrate ([3000, 2000, 1000]);
If you want to stop shaking, you just need to pass in 0 to the Navigator.vibrate method, or an empty array:
Stop Vibration
Copy Code code as follows:
Navigator.vibrate (0);
Navigator.vibrate ([]);
It should be recalled that the call to the Navigator.vibrate method does not cause the phone to vibrate; When the parameter is a number, the vibration occurs once and then stops. When the argument is an array, the vibrations vibrate according to the values in the array, and then stop the vibration.
Continuous vibration
We can simply use the SetInterval and Clearinterval methods to produce the effect that keeps the phone vibrating:
var vibrateinterval;
Starts vibration at passed on level
function startvibrate (duration) {
navigator.vibrate (duration);
}
Stops Vibration
Function stopvibrate () {
//clear interval and stop persistent vibrating
if ( Vibrateinterval) clearinterval (vibrateinterval);
Navigator.vibrate (0);
}
Start persistent vibration at given duration and interval
//assumes a number value is given
function Startpe Ristentvibrate (Duration, interval) {
vibrateinterval = setinterval (function () {
startvibrate (duration);
} , interval);
}
The above code is only for the case where the vibration parameter is a number, and if the argument is an array, you need to calculate its total duration and then loop it according to its characteristics.
Scenarios using the vibration (vibration) API
This API is clearly targeted at mobile phone devices. When developing mobile Web mobile applications, it is a good warning tool, when the development of web games or multimedia applications, this vibration function is an indispensable good technology. For example, is it an excellent user experience when a user plays your web game with a mobile phone, when the game explodes and you let the phone vibrate with it?
How do you feel about this JavaScript vibration API? Think it's going to pop up fast? Or is it not much use?