How it works
The Vibration API is a single method. It takes one duration, or an array of durations in milliseconds that alternate between vibrating and pausing:
navigator.vibrate(200); // one 200ms buzz
navigator.vibrate([30, 60, 30]); // buzz, pause, buzz
navigator.vibrate(0); // stopA new call replaces whatever is still playing, and there's no option to loop. To repeat a pattern you write it out several times, which is what repeat does here.
There's also no intensity setting. The API only switches the motor on and off, and how strong or sharp a buzz feels depends on the phone. Short pulses feel crisp on a good motor and can be too weak to notice on a cheap one, so test on more than one device.
Where it works
- Android: Chrome, Edge, Samsung Internet and other Chromium browsers.
- iOS: no browser. Safari never implemented the API, and every iOS browser runs on Safari's engine, so Chrome on an iPhone doesn't have it either.
- Firefox: removed on all platforms in version 129 (2024). It had done nothing on Android for years, and keeping the method made feature detection lie.
- Desktop: Chrome exposes the method and returns
true, but there's no motor to run. That's why this page draws the pattern and plays it as a low buzz instead.
Rules browsers enforce
- The user has to interact first. Chrome ignores
vibrate()until the user has tapped or clicked the page and logs a warning in the console. Call it from an event handler. - Only visible pages vibrate. Switching tabs or locking the screen stops the pattern.
The iOS fallback
Safari 17.4 added a native switch, <input type="checkbox" switch>, and on iOS 18 toggling it plays the same short haptic tick as a system switch. The fallback in the generated code creates a hidden switch and clicks it once per buzz. It can't control duration, so a long buzz feels the same as a short one, and since it relies on a side effect rather than an API, any iOS update can stop it. Treat it as a bonus, not a feature.
Using vibration well
- Short and rare. 10 to 50ms is a tap. Anything over about 300ms reads as an alert. Save vibration for moments that matter, such as a completed action, an error or a long press, not every scroll or hover.
- Make the patterns different by rhythm. Users can't tell 40ms from 60ms, but they can tell one buzz from two or three. Success and error should differ in count, not in length.
- Never carry the only signal. Many devices have no vibration, and people turn it off. Every buzz needs a visual change next to it.
- Let people turn it off, and remember the choice. There's no media query for haptics, but
prefers-reduced-motionis a reasonable hint that someone wants less of it.