Implement vibration in ReactJS

I want to make the device vibrate with some part of my code (not relevant), I have seen some methods for js but I can't get it to work in react. Here is what I have tried so far inside of a function:

window.navigator.vibrate(200);
navigator.vibrate([1000,      500,    1000]);
navigator.vibrate(Infinity); // Infinity is a number

Upvotes: 0

Views: 4882

Answers (1)

goudarziha
goudarziha

Reputation: 336

Check to make sure vibrate is supported in your current browser

if ("vibrate" in navigator) {
	// vibration API supported
  navigator.vibrate(1000);
}

or

// enable vibration support
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;

if (navigator.vibrate) {
	// vibration API supported
    navigator.vibrate(1000);
}

Upvotes: 2

Related Questions