JavaScript mobile phone Vibration api_javascript Skills

Source: Internet
Author: User
Tags numeric setinterval

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:

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
navigator.vibrate (1000);

Vibration multiple
//parameters are vibration 3 seconds, wait 2 seconds, then vibrate 1 seconds
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
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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.