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.
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:
Vibrates for 200 milliseconds
Will cancel any currently ongoing vibration pattern
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