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 on the implementation.

句法

var successBool = window.navigator.vibrate(pattern);
					
pattern
Provides a pattern of vibration and pause intervals. Each value indicates a number of milliseconds to vibrate or pause, in alternation. You may provide either a single value (to vibrate once for that many milliseconds) or an array of values to alternately vibrate, pause, then vibrate again. See 振动 API 了解细节。

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,30,200,30,200,30,200,30,100,30,100,30,100]); // Vibrate 'SOS' in Morse.
					

规范

规范 状态 注释
振动 API 推荐 Linked to spec is the latest editor's draft; W3C version is a REC.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
vibrate Chrome 32 Edge ≤79 Firefox 16
16
Until Firefox 26 included, when the vibration pattern was too long or any of its elements too large, Firefox threw an exception instead of returning false ( bug 884935 ).
From Firefox 32 onwards, when the vibration pattern is too long or any of its elements too large, it returns true but truncates the pattern ( bug 1014581 ).
Beginning in Firefox 72, this is not supported in cross-origin iframes.
11 Prefixed
Prefixed Implemented with the vendor prefix: moz
IE No Opera No Safari No WebView Android 4.4.3
4.4.3
Beginning in version 55, this is not supported in cross-origin iframes.
Beginning in version 60, this method requires a user gesture. Otherwise it returns false .
Chrome Android 32
32
Beginning in Chrome 55, this is not supported in cross-origin iframes.
Beginning in Chrome 60, this method requires a user gesture. Otherwise it returns false .
Firefox Android 16
16
Until Firefox 26 included, when the vibration pattern was too long or any of its elements too large, Firefox threw an exception instead of returning false ( bug 884935 ).
From Firefox 32 onwards, when the vibration pattern is too long or any of its elements too large, it returns true but truncates the pattern ( bug 1014581 ).
14 Prefixed
Prefixed Implemented with the vendor prefix: moz
Opera Android Yes
Yes
Beginning in Opera 47, this method requires a user gesture. Otherwise it returns false .
Safari iOS No Samsung Internet Android 2.0
2.0
Beginning in Samsung Internet 6.0, this is not supported in cross-origin iframes.
Beginning in Samsung Internet 8.0, this method requires a user gesture. Otherwise it returns false .

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: