Vibration API coming to Firefox 11
January 27th, 2012The current Aurora release of Firefox includes the Vibration API, a very simple system for making devices rumble.
The API consists of a single call to navigator.mozVibrate().
The function accepts a single parameter. Pass a positive integer to indicate the number of milliseconds the device should vibrate. Pass an array of these integers to create a vibrate pattern. Finally, pass 0 to stop any current vibration.
navigator.mozVibrate(2000); //Vibrate for 2 seconds navigator.mozVibrate([1000, 1000, 500]); //Vibrate for a second. //Then pause for a second. //Then vibrate for another half-second. navigator.mozVibrate(0); //An empty array is also valid //Stop vibrating
Obviously, this is only meaningful on devices that support vibration, like mobile phones, tablets and game controllers. However, in both the Firefox implementation and pending W3C specification, calls are silently ignored when the device does not support vibration or vibration is not allowed.
The W3C added a NotSupportedError that is thrown when invalid values are passed to the function. Obviously, it has also generalized mozVibrate() to navigator.vibrate().
The specification also checks document.hidden before the rumble begins and at any time the visibilitychange event is triggered. If document.hidden is false, any requested vibrations must be ignored and any current vibration must cease immediately. No feedback is provided to your code in either of these events.
This means that spam pages can’t drain your battery (unless you leave the offending page up), a concern expressed on the WebKit mailing list and is implemented as navigator.webkitVibrate() in the development builds of the WebKit core.


