Created
December 16, 2021 10:43
-
-
Save aminnairi/432c79f892bda45c62e5a0222fd898b6 to your computer and use it in GitHub Desktop.
Vibration API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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