DEV Community

Cover image for Vibrating your phone with JavaScript 📱
Dhairya Shah
Dhairya Shah

Posted on Originally published at codewithsnowbit.hashnode.dev

Vibrating your phone with JavaScript 📱

In this article we are going to vibrate mobile phones with JavaScript Vibration API.

JavaScript is an amazing language

Navigator.vibrate()

The navigator.vibrate method pulses the vibration hardware on the device if such device is compatible. If device is not compatible, no effect will take place.

The method works in the following structure,

navigator.vibrate(pattern)
📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung 📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung

So, this was a little explanation on the navigator.vibrate method. Now let's deal with some easy examples.

Examples

const vibrate = () => {
  window.navigator.vibrate([500])
}
📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung 📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung

This will vibrate the hardware for 500ms.

const vibrateTwoTimes = () => {
  window.navigator.vibrate([200, 100, 200])
}
📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung 📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung

In this example, vibration will occur two times for 200ms with a gap of 100ms.

Conclusion

I hope you have found this article useful. Thank you for reading!

Let's Connect

Top comments (8)

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
khokon profile image
Khokon M. •

hahaha, seems fun. Thanks for sharing

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
dhairyashah profile image
Dhairya Shah •

Thanks for reading 🙌

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
glowreeyah profile image
Gloria Asuquo •

This was fun to try out
Thank you

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
dhairyashah profile image
Dhairya Shah •

You're welcome 🙂

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
stefino76 profile image
Stefano Baccetti •

Awesome! 🤩

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
stcanpham profile image
Can Pham T. •

Thanks for sharing.

I use vibration with the touch event, but on the first page load, after the first touch, vibration does not work. It only works from the second touch onward.

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
omriluz profile image
omriluz •

Yeah since a user has to interact with your page before you can use this API

📺 Info Siaran: dhairyashah/vibrating your phone with javascript b - Jadwal Siaran Langsung
 
anuj_pandey_3c5127830462d profile image
Anuj Pandey •

This is not working in iphone browser