Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptWeb Vibration API | Navigator.vibrate() Method

Web Vibration API | Navigator.vibrate() Method

The Navigator.vibrate() method uses the vibration hardware on the device, if there is any such hardware which exists. If the device doesn’t support vibration then this method will not be affected. If a vibration pattern has already started when this method was called, the previous pattern will be stopped and the new one will start instead.

If we put invalid parameters inside the method then it will not vibrate, and it will return false, else it returns true. If the vibration is too long from our pattern, then it is truncated. The maximum length depends on the implementation.

Syntax:

var successBool = window.navigator.vibrate( pattern );

Parameters: This method accepts single parameter pattern which provides a pattern for vibration and pause intervals. Both vibration and interval values are in alternation and values are in milliseconds. We either can provide a single value or array of values. Passing 0, an empty array, or an array of all zeroes as a parameter will cancel any current ongoing vibration.

Return Value: It returns True on success otherwise returns False.

Below examples illustrate Navigator.vibrate() method in HTML Vibration API:

Example 1:




// To check that is vibration API supported
if (navigator.vibrate) {
    window.navigator.vibrate(200);
}


Output:

Vibrates for 200 milliseconds

Example 2:




// To check that is vibration API supported
if (navigator.vibrate) {
    window.navigator.vibrate(0);
}


Output:

Will cancel any currently ongoing vibration pattern

Example 3:




// To check that is vibration API supported
if (navigator.vibrate) {
    window.navigator.vibrate([100, 30, 100, 30, 100, 30, 200, 
                     30, 200, 30, 200, 30, 100, 30, 100, 30, 100]);
}


Output:

Vibrate 'SOS' in Morse

Supported Browsers: The browsers supported by Web Vibration API Navigator.vibrate() Method are listed below:

  • Google Chrome 32 or above
  • Firefox 16 or above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments