← Plugins / Vibration
This Apache Cordova plugin lets your app deliver haptic feedback — a single buzz or a custom rhythm of buzzes. It's a simple way to confirm an action, flag an alert, or add tactile punch to a game.
Add the plugin to your Cordova project from the command line:
cordova plugin add cordova-plugin-vibration
The plugin extends the global navigator object with a vibrate function. Call it after deviceready has fired:
| Call | Description |
|---|---|
navigator.vibrate(time) | Vibrates for time milliseconds. |
navigator.vibrate([t1, t2, t3, ...]) | Plays an Android pattern of alternating wait and vibrate durations, in milliseconds. |
navigator.vibrate(0) or navigator.vibrate([]) | Cancels any vibration that is currently running (Android). |
On iOS the duration and any pattern are disregarded — the device always plays its single, standard short vibration. Custom patterns and cancellation are available on Android only.
Fire a quick confirmation buzz, or play a short two-part pattern:
document.addEventListener("deviceready", onReady, false);
function onReady() {
// A quick confirmation buzz
navigator.vibrate(200);
// An Android pattern: wait 0ms, buzz 200ms, wait 100ms, buzz 200ms
navigator.vibrate([0, 200, 100, 200]);
}
navigator.vibrate after deviceready — it isn't available before then.