Vibration API in JavaScript
Request a buzz-pause-buzz pattern in JavaScript with navigator.vibrate after a click. Check support; an accepted request does not guarantee a felt vibration.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="buzz">Test vibration</button><p id="status"></p>7<script>8document.querySelector('#buzz').onclick = () => {9 const status = document.querySelector('#status');10 if (!('vibrate' in navigator)) { status.textContent = 'Vibration is not supported on this device/browser.'; return; }11 const accepted = navigator.vibrate([100, 50, 100]);12 status.textContent = accepted ? 'Vibration requested: buzz, pause, buzz.' : 'Vibration request refused.';13};14</script>15</body>16</html>
Sample Output
Vibration requested: buzz, pause, buzz.
The browser accepted the pattern request. It does not confirm physical vibration.
In this one
- Check vibration support
- Read a buzz-pause pattern
- Keep feedback visible as text
- Avoid promising hardware output
How it works
- 1
Wait for the Test vibration click.
- 2
Check whether vibrate exists on navigator.
- 3
Request a 100 ms buzz, 50 ms pause, and 100 ms buzz.
- 4
Inspect the returned boolean and show a message.
- 5
Compare accepted, refused, or unsupported text with actual device behavior.
Edge cases
Unsupported browser
Many browsers do not expose vibrate.
The support branch prints a message rather than calling a missing function.
Accepted but not felt
Hardware and device settings can make the result silent.
Say requested, not delivered. Keep the text feedback so the action is still understandable.
What's going on
The Vibration API asks a supported device to vibrate. An API is a feature JavaScript can call. A short vibration can provide extra feedback after a button press, but it should never be the only way to report important information.
This browser-only example checks whether navigator.vibrate exists before calling it. The array describes a pattern in milliseconds: vibrate for 100, pause for 50, then vibrate for 100. A millisecond is one thousandth of a second. The request happens after a real click.
The boolean result says whether the browser accepted the request, not that the person felt it. Devices without suitable hardware, device settings, or browser rules can prevent a noticeable vibration. Many desktop and mobile browser combinations do not support this feature. The text message gives feedback even when no vibration occurs, and navigator.vibrate(0) can cancel an ongoing pattern.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is 50 in [100, 50, 100]?
Pick an answer to check it.
Question 2
Does true prove the person felt it?
Pick an answer to check it.
Questions people ask
What do the array numbers mean?
They alternate vibration and pause durations, starting with vibration, in milliseconds.
Can I use this on a laptop?
Only if both browser and hardware support it. Most laptop setups will not produce a vibration.
How do I stop a long pattern?
Call navigator.vibrate(0) in a supported browser.