Make iphone/smartphone vibrate from website, is it possible?

19,180

Solution 1

You can always use Phonegap/Cordova to pack the web content into a native app. Then you'll have access to all (or most) of the native phone features.

Bear in mind that using JS for vibration is still widely unsupported while the native thing will work on everything.

Solution 2

Yes it is, using HTML5's Vibration API. To make the phone vibrate, use:

navigator.vibrate(1000);

However before executing this statement, you should check whether the browser supports the Vibration API. You can do this either this way:

if (window.navigator && window.navigator.vibrate) {
   // Vibration supported
} else {
   // Vibration not supported
}

Or this way:

if ('vibrate' in navigator) {
   // Vibration supported
} else {
   // Vibration not supported
}

You can also define a vibration pattern. This is possible by passing an array with [vibration, pause, vibration, pause...], like so:

// Vibrate for 3 seconds, pause for half a second, vibrate for 2 seconds, pause for half a second, vibrate for 1 second    
navigator.vibrate([3000, 500, 2000, 500, 1000]);

To make the phone stop vibrating, you can use either:

navigator.vibrate(0);

Or:

navigator.vibrate([]);

Source: tutplus

Share:
19,180
Legarndary
Author by

Legarndary

Hello :-) My name is Kasper, I'm from Denmark and I'm a front-end deveolper (Y)

Updated on June 24, 2022

Comments

  • Legarndary
    Legarndary almost 2 years

    So im buiding a little quiz game using php, jQuery etc. I have maded it so each player uses his or hers smartphone as a "buzzer" button. And the i was wondering if it is possible to make the phone vibrate to give the user a nicer feedback of pressen the button.

    I found a vibrate API but iOS is not supporting it (i have an iphone to test it)

    It is the only thing i could found about it, someone know something else?

  • Legarndary
    Legarndary over 9 years
    You are right, but it is really only ment to be a little game for and my friends and dont want to pay the cost of getting an app on appstore googlemarket etc. for just a little private thing :)
  • Shomz
    Shomz over 9 years
    Yeah, I totally understand. But I'm not sure you have another choice, at least for now. Btw. you don't have to publish the app in order to use it on the phone (it's super-simple on Androids, a bit more difficult on iPhones).
  • gidzior
    gidzior over 6 years
    is there a way to vibrate in IOS ??