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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Vibrates for 200 milliseconds

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Will cancel any currently ongoing vibration pattern

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
}

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.