MyInternships.in

HTML APIs

What is the Vibration API in HTML5?

The HTML5 Vibration API lets a web page make a mobile device vibrate through a single call, navigator.vibrate(), giving tactile haptic feedback for taps, alerts and game events.


What is the Vibration API and when to use it?

The Vibration API gives web apps access to a device's vibration hardware, if it has any. It is a tiny API - essentially one method, navigator.vibrate() - designed for haptic (touch) feedback. It targets mobile devices; most desktops have no vibration motor, so the call simply does nothing there.

Reach for it to add a subtle buzz when a button is pressed, an alert when a message arrives, or feedback for events in a mobile browser game. Treat it as a non-essential enhancement, never as the only way to convey something important.

Syntax

Call navigator.vibrate(duration) with a number of milliseconds to vibrate once, or pass an array to describe a pattern. navigator.vibrate(0) or navigator.vibrate([]) cancels any ongoing vibration. The method returns true if the request was accepted, false otherwise.

The three ways to call vibrate()
navigator.vibrate(200);              // buzz once for 200 ms
navigator.vibrate([100, 30, 200]);   // buzz 100, pause 30, buzz 200
navigator.vibrate(0);                // cancel any current vibration

Example: buzz the device on a tap

Paste this complete page into the editor and open it on an Android phone in Chrome, then tap the button. On desktop or unsupported browsers the code feature-detects and shows a message instead of failing.

Runnable: vibrate on a button tap
Example
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Vibration demo</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 24px; }
    button { padding: 12px 20px; font-size: 16px; cursor: pointer; margin: 6px 0; display: block; }
    #status { margin-top: 14px; color: #475569; }
  </style>
</head>
<body>
  <h3>Tap to vibrate</h3>
  <button id="short">Short buzz (200ms)</button>
  <button id="pattern">Pattern buzz</button>
  <button id="stop">Stop</button>
  <div id="status"></div>

  <script>
    const status = document.getElementById("status");
    const supported = "vibrate" in navigator;
    status.textContent = supported
      ? "Vibration is supported - tap a button (mobile only)."
      : "Vibration API is not supported on this device/browser.";

    document.getElementById("short").addEventListener("click", () => {
      if (supported) navigator.vibrate(200);
    });
    document.getElementById("pattern").addEventListener("click", () => {
      if (supported) navigator.vibrate([100, 30, 200, 30, 100]);
    });
    document.getElementById("stop").addEventListener("click", () => {
      if (supported) navigator.vibrate(0);
    });
  </script>
</body>
</html>

More examples: vibration patterns

Pass an array to build a pattern of alternating vibrate/pause durations in milliseconds. Values at even indexes are vibrations, values at odd indexes are pauses. This lets you create buzz-pause-buzz sequences, like a Morse-style alert or a double-tap confirmation.

Patterns and cancellation
// vibrate 100, pause 30, vibrate 200, pause 30, vibrate 100
navigator.vibrate([100, 30, 200, 30, 100]);

// cancel any current or queued vibration
navigator.vibrate(0);

// a simple "double tap" confirmation buzz
function confirmBuzz() {
  navigator.vibrate([50, 40, 50]);
}

navigator.vibrate() at a glance

CallEffect
navigator.vibrate(200)Vibrate once for 200 milliseconds
navigator.vibrate([100, 30, 100])Vibrate 100ms, pause 30ms, vibrate 100ms
navigator.vibrate(0)Stop / cancel any current vibration
navigator.vibrate([])Also cancels any current vibration
'vibrate' in navigatorFeature-detect before calling
⚠️

Mobile-only and gesture-gated: vibration works only on devices with a vibration motor (mostly Android phones). Browsers require the call to happen in response to a user gesture such as a tap - programmatic calls with no interaction are usually ignored, and a backgrounded page cannot vibrate.

ℹ️

Support is uneven. It works in Chrome and other Chromium browsers on Android, but Safari on iOS does NOT support navigator.vibrate at all. On desktop the API may exist but has no hardware to drive, so it silently does nothing. Always feature-detect with 'vibrate' in navigator and treat vibration as a non-essential enhancement.

💡

Keep vibrations short and infrequent. Long or repeated buzzing drains the battery and annoys users, and some browsers cap the maximum duration. Never rely on vibration for critical information, since many devices and browsers cannot deliver it.

Key Takeaways

  • The whole API is one method: navigator.vibrate(), for haptic feedback on mobile.
  • Pass a number to buzz once, or an array to build a vibrate/pause pattern.
  • navigator.vibrate(0) or an empty array cancels any ongoing vibration.
  • It is gesture-gated: the buzz must be triggered by a user tap, not run automatically.
  • Support is patchy (no iOS Safari, no desktop hardware) - feature-detect and treat it as optional polish.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships