Navigator.vibrate()

  • Revision slug: Web/API/Navigator.vibrate
  • Revision title: Navigator.vibrate()
  • Revision id: 458315
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Moved From Web/API/window.navigator.vibrate to Web/API/Navigator.vibrate

Revision Content

{{DomRef("window.navigator")}}

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 (e.g. too large value), it will return false (though Gecko device incorrectly throw for the moment), else it returns true.

Syntax

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 Vibration API for details.

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

Examples

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.

Specifications

Specification Status Comment
{{SpecName('Vibration API')}} {{Spec2('Vibration API')}} Initial specification.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{property_prefix("webkit")}} {{CompatGeckoDesktop("11.0")}} {{property_prefix("moz")}}
{{CompatGeckoDesktop("16.0")}} (no prefix) [1]
{{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{property_prefix("webkit")}} {{CompatGeckoMobile("11.0")}} {{property_prefix("moz")}}
{{CompatGeckoMobile("16.0")}} (no prefix) [1]
{{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] When the vibration pattern is too long or any of its elements are too large, Gecko incorrectly throws an exception, instead of returning true ({{bug("884935")}}).

See also

Revision Source

<div>
  {{DomRef("window.navigator")}}</div>
<p>The <strong><code>Navigator.vibrate()</code></strong> 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.</p>
<p>If the method was unable to vibrate because of invalid parameters (e.g. too large value), it will return <code>false</code> (though Gecko device incorrectly throw for the moment), else it returns <code>true</code>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
window.navigator.vibrate(<var>pattern</var>);
</pre>
<dl>
  <dt>
    <code>pattern</code></dt>
  <dd>
    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 <a href="/en-US/docs/WebAPI/Vibration" title="/en-US/docs/WebAPI/Vibration">Vibration API</a> for details.</dd>
</dl>
<p>Passing a value of <code>0</code>, an empty array, or an array containing all zeros will cancel any currently ongoing vibration pattern.</p>
<h2 id="Examples">Examples</h2>
<pre class="brush: js">
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.
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('Vibration API')}}</td>
      <td>{{Spec2('Vibration API')}}</td>
      <td>Initial specification.</td>
    </tr>
  </tbody>
</table>
<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>{{CompatGeckoDesktop("11.0")}} {{property_prefix("moz")}}<br />
          {{CompatGeckoDesktop("16.0")}} (no prefix) [1]</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>{{CompatGeckoMobile("11.0")}} {{property_prefix("moz")}}<br />
          {{CompatGeckoMobile("16.0")}} (no prefix) [1]</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>[1] When the vibration pattern is too long or any of its elements are too large, Gecko incorrectly throws an exception, instead of returning <code>true</code> ({{bug("884935")}}).</p>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/WebAPI/Vibration" title="/en-US/docs/WebAPI/Vibration">Vibration API</a></li>
</ul>
Revert to this revision