JavaScript Reference ยท Browser and Web APIs
Vibration and other small device APIs
Reference for navigator.vibrate(), navigator.onLine, hardwareConcurrency and the Screen Wake Lock API: what each returns, when it fails and how to detect support.
Besides the big APIs (camera, location, notifications), browsers expose a handful of small ones that read a fact about the device or ask it for a tiny favor. navigator.vibrate() buzzes the device, navigator.onLine says whether the browser thinks it is online, navigator.hardwareConcurrency reports the number of logical processors, and the Screen Wake Lock API keeps the screen from dimming.
None of them is guaranteed to exist, so the rule for every one is the same: feature-detect first, treat the answer as a hint, and have a working page without it.
if ("vibrate" in navigator) { navigator.vibrate([100, 50, 100]); // buzz, pause, buzz (milliseconds) }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
navigator.vibrate(pattern) navigator.onLine navigator.hardwareConcurrency const lock = await navigator.wakeLock.request("screen")
| Name | Description |
|---|---|
pattern |
A number of milliseconds, or an array alternating vibration and pause durations. 0 or [] cancels vibration. |
"screen" |
The only wake lock type; keeps the screen on while the lock is held. |
Return value: vibrate() returns a boolean (true if the pattern was accepted). onLine is a boolean. hardwareConcurrency is an integer. wakeLock.request() returns a promise for a WakeLockSentinel, whose release() method gives the lock back.
Exceptions: wakeLock.request() rejects with NotAllowedError when the page is hidden, not allowed, or the battery policy refuses. vibrate() does not throw for unsupported devices; it simply has no effect.
Usage notes
vibrate()needs user activation in most browsers. Call it from a tap or click handler, not on page load.- Long patterns are capped. Engines limit total length and individual durations, so keep patterns short.
- Vibration is not available everywhere. Desktop browsers usually ignore it, and some mobile browsers do not implement it at all. Never use it as the only way to tell the user something.
navigator.onLinecan be wrong in one direction.falsereliably means no network connection, buttrueonly means the device is connected to a network, not that your server is reachable. Handle failedfetch()calls anyway.hardwareConcurrencyis a sizing hint. It is useful for choosing how many Web Workers to start, but browsers may cap or round it for privacy.- Wake locks are released automatically when the page becomes hidden, so request again on
visibilitychangeif you still need one. - Respect the user. Keep the screen awake only while it helps (reading a recipe, a presentation), and release the lock when done.
Browser support
navigator.onLine, hardwareConcurrency and vibrate() on mobile are widely supported, but vibrate() is missing in some browsers. The Screen Wake Lock API is newer. Check a compatibility table for each.