How to make a cellphone vibrate using JavaScript
Using the Vibration API. Even though it is still not fully standardized, Firefox 11+ and Chrome (both for mobile) already support it. Internet Explorer's implementation is under consideration. Its use is very simple:
//Verify the existence of the vibrate function if(Modernizr.vibrate) { //Vibrates for one second window.navigator.vibrate(1000); //Vibrates for one, two and then three seconds, with short pauses between each vibration window.navigator.vibrate([1000, 2000, 3000]); } else { //No support yet, try using other browser }
Two other things are also worth mentioning:
The vibrate function returns a boolean that indicates whether it was executed correctly or not.
When you call the vibrate function and the device is still vibrating from a previous call, the new call will halt the previous one. It means that calling the vibrate function and passing it a 0 will make the device stop vibrating, so it can be used to implement a "stopVibration" sort of function.