---
title: "Cordova - Vibration"  
description: "There are times when vibration is needed into the app, such as when back button is pressed, on display of any alert or warning message, etc. This plug"  
author: "Anonymous User"  
published: 2017-05-04  
updated: 2018-03-19  
canonical: https://www.mindstick.com/blog/11375/cordova-vibration  
category: "apache cordova"  
tags: ["mobile development", "apache cordova"]  
reading_time: 2 minutes  

---

# Cordova - Vibration

There are times when vibration is needed into the app, such as when back button is pressed, on display of any alert or warning message, etc. This plugin will help you to connect to device's vibration functionality.

**Step 1 - [Install](https://www.mindstick.com/articles/12876/the-top-5-reasons-to-buy-and-install-a-tonneau-cover) Vibration Plugin**

Open the [command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt) window and run the following code to install this plugin:

```
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-vibration
```

**Step 2 - Adding Buttons**

When the plugin is installed you can add buttons in index.html file that will be used later to execute the vibration.

```
<button id = "vibrationmode">VIBRATION</button>
<button id = "Patternofvibration">PATTERN</button>
```

**Step 3 - Adding [Event Listeners](https://www.mindstick.com/forum/159117/how-do-you-handle-events-using-jquery-and-attach-event-listeners-to-elements)**

Now, in index.js file, inside onDeviceReady, you can [add event](https://answers.mindstick.com/qa/93763/how-to-add-event-handlers-on-html-elements-by-javascript) listeners.

```
document.getElementById("vibrationmode").addEventListener("click", vibrationmode);
document.getElementById("Patternofvibration").addEventListener("click", Patternofvibration);
```

\

**Step 4 - Create [Functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing)**

This plugin can be very easily used. You have to create two functions.

```
function vibrationmode() {
   var time = 3000;
   navigator.vibrate(time);
}
function Patternofvibration () {
   var pattern = [1000, 1000, 1000, 1000];
   navigator.vibrate(pattern);
}
```

The first [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is taking **time** [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) that is used for setting the [duration](https://answers.mindstick.com/qa/45301/what-is-a-short-duration-discussion) of the vibration. When we press the **VIBRATION** button the device will vibrate for three seconds.

The second function have **pattern** parameter. This array will tell the device to have break of one second between the consecutive vibrates.

**Also Read:** [Cordova- Whitelist](https://www.mindstick.com/blog/11373/cordova-whitelist)

[Cordova- Best Practices](https://www.mindstick.com/blog/11374/cordova-best-practices)\

---

Original Source: https://www.mindstick.com/blog/11375/cordova-vibration

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
