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