The Navigator.vibrate()
method pulses the vibration hardware on the device, if such hardware exists. If the device doesn't support vibration, this method has no effect. If a vibration pattern is already in progress when this method is called, the previous pattern is halted and the new one begins instead.
If the method was unable to vibrate because of invalid parameters, it will return false
, else it returns true
. If the pattern leads to a too long vibration, it is truncated: the max length depends of each implementation.
var successBool = window.navigator.vibrate(pattern);
Passing a value of 0
, an empty array, or an array containing all zeros will cancel any currently ongoing vibration pattern.
window.navigator.vibrate(200); // vibrate for 200ms window.navigator.vibrate([100,30,100,30,100,200,200,30,200,30,200,200,100,30,100,30,100]); // Vibrate 'SOS' in Morse.
Specification | Status | Comment |
---|---|---|
Vibration API | Recommendation | Linked to spec is the latest editor's draft; W3C version is a REC. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | No support |
11.0 (11.0) moz 16.0 (16.0) (no prefix) [1] | No support | No support | No support |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support |
(Yes) webkit (Yes) (unprefixed) [2][3] |
(Yes) webkit (Yes) (unprefixed) [2][3] | 11.0 (11.0) moz 16.0 (16.0) (no prefix) [1] | No support | (Yes)[3] | No support |
[1] When the vibration pattern is too long or any of its elements are too large, until Firefox 26 included, Gecko threw an exception, instead of returning false
(bug 884935). From Firefox 32 onwards, it returns true
but truncates the pattern (bug 1014581).
[2] Beginning in Chrome 55, this is not supported in cross-origin iframes.
[3] Beginning in Chrome 60/Opera 47, this method requires a user gesture. Otherwise it returns false
.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/navigator/vibrate