Skip to content

Instantly share code, notes, and snippets.

@aminnairi
Created December 16, 2021 10:43
Show Gist options
  • Save aminnairi/432c79f892bda45c62e5a0222fd898b6 to your computer and use it in GitHub Desktop.
Save aminnairi/432c79f892bda45c62e5a0222fd898b6 to your computer and use it in GitHub Desktop.
Vibration API
// Si le navigateur n'a pas implémenté correctment l'API de Vibration
if (!window.navigator.vibrate || typeof window.navigator.vibrate !== "function") {
const message = "Vibration not supported by this device, sorry!";
alert(message);
throw new Error(message);
}
// Récupération d'un bouton pour pouvoir faire vibrer l'appareil
const vibrateButton = window.document.getElementById("vibrate");
// Si le bouton n'est pas dans le DOM
if (!vibrateButton) {
const message = "Vibration button not found";
alert(message);
throw new Error(message);
}
// Lorsque le bouton est cliqué
vibrateButton.addEventListener("click", () => {
// Fait vibrer l'appareil avec un motif particulier
window.navigator.vibrate([200, 100, 200]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment