Example 1 — Short Pulse (MDN)
Vibrate once for 200 milliseconds from a button click.
document.querySelector("button").addEventListener("click", () => {
if (typeof navigator.vibrate !== "function") {
console.log("Vibration API not supported");
return;
}
const ok = navigator.vibrate(200);
console.log(ok ? "vibrate(200) accepted" : "vibrate(200) rejected");
}); How It Works
- Feature-detect so missing APIs fail quietly.
- Wire a click — sticky user activation is required (MDN).
- Pass one number — vibrate that many milliseconds once.
- Check the boolean —
truemeans params were valid, not that you felt a buzz.

