Navigator.vibrate()

  • Revision slug: DOM/window.navigator.vibrate
  • Revision title: window.navigator.vibrate
  • Revision id: 349609
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{DomRef}}{{SeeCompatTable}}

Summary

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.

Syntax

window.navigator.vibrate(pattern);
  • pattern provides a pattern of vibration and pause intervals, as described below.

Describing vibrations

The pattern may be either a single integer, describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses.

A single vibration

You may pulse the vibration hardware one time by specifying either a single value, or an array consisting of only one value:

window.navigator.vibrate(200);
window.navigator.vibrate([200]);

Both of these examples vibrate the device for 200ms.

Vibration patterns

An array of values describes alternating periods of time in which the device is vibrating and not vibrating. Each value in the array is converted to an integer, then interpreted alternately as the number of milliseconds the device should vibrate and the number of milliseconds it should not be vibrating. For example:

window.navigator.vibrate([200, 100, 200]);

This vibrates the device for 200ms, then pauses for 100ms before vibrating the device again for another 200ms.

You may specify as many vibration/pause pairs as you like, and you may provide either an even or odd number of entries; it's worth noting that you don't have to provide a pause as your last entry since the vibration automatically stops at the end of each vibration period.

Canceling existing vibrations

Passing a value of 0, an empty array, or an array containing all zeros will cancel any currently ongoing vibration pattern.

Exceptions thrown

An exception may be thrown if the vibration pattern is too long or any of its elements are too large.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{property_prefix("webkit")}}

11.0 {{property_prefix("moz")}}

16 (no prefix)

{{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{property_prefix("webkit")}} 11.0 {{property_prefix("moz")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

Specification

  • {{spec("http://www.w3.org/TR/vibration/", "Vibration API Specification", "WD")}}

Revision Source

<div>
  {{DomRef}}{{SeeCompatTable}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>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.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
window.navigator.vibrate(<var>pattern</var>);
</pre>
<ul>
  <li><code>pattern</code> provides a pattern of vibration and pause intervals, as described below.</li>
</ul>
<h2 id="Describing_vibrations">Describing vibrations</h2>
<p>The pattern may be either a single integer, describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses.</p>
<h3 id="A_single_vibration">A single vibration</h3>
<p>You may pulse the vibration hardware one time by specifying either a single value, or an array consisting of only one value:</p>
<pre class="brush:js">
window.navigator.vibrate(200);
window.navigator.vibrate([200]);
</pre>
<p>Both of these examples vibrate the device for 200ms.</p>
<h3 id="Vibration_patterns">Vibration patterns</h3>
<p>An array of values describes alternating periods of time in which the device is vibrating and not vibrating. Each value in the array is converted to an integer, then interpreted alternately as the number of milliseconds the device should vibrate and the number of milliseconds it should not be vibrating. For example:</p>
<pre class="brush: js">
window.navigator.vibrate([200, 100, 200]);
</pre>
<p>This vibrates the device for 200ms, then pauses for 100ms before vibrating the device again for another 200ms.</p>
<p>You may specify as many vibration/pause pairs as you like, and you may provide either an even or odd number of entries; it's worth noting that you don't have to provide a pause as your last entry since the vibration automatically stops at the end of each vibration period.</p>
<h3 id="Canceling_existing_vibrations">Canceling existing vibrations</h3>
<p>Passing a value of 0, an empty array, or an array containing all zeros will cancel any currently ongoing vibration pattern.</p>
<h2 id="Exceptions_thrown">Exceptions thrown</h2>
<p>An exception may be thrown if the vibration pattern is too long or any of its elements are too large.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}} {{property_prefix("webkit")}}</td>
        <td>
          <p>11.0 {{property_prefix("moz")}}</p>
          <p>16 (no prefix)</p>
        </td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}} {{property_prefix("webkit")}}</td>
        <td>11.0 {{property_prefix("moz")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li>{{spec("http://www.w3.org/TR/vibration/", "Vibration API Specification", "WD")}}</li>
</ul>
Revert to this revision