apache / cordova-plugin-vibration

Apache Cordova Vibration Plugin
https://cordova.apache.org/
Apache License 2.0
127 stars 129 forks source link

What does "if user hasn't tapped on the frame or any embedded frame yet." Mean? #117

Closed Andalusio closed 6 months ago

Andalusio commented 6 months ago

This issue is with Android, and not even Google Gemini could help figure out how to get navigate.vibrate to work by adding "touchend" via "addEventListener." I feel like this will never be available on Android, and I needed this for a gimmick in one of my games.

And what is this "frame" the readme speaks of?

Andalusio commented 6 months ago

Never mind. With help from Google Gemini, I figured a way of just touching the screen in general to trigger vibration. In case you're interested, here is an example:

var isUserInteracted = false; // Flag to track user interaction

document.addEventListener("touchstart", function() { isUserInteracted = true; // Set flag upon user interaction });

var intervalId = null; // Variable to store the interval ID

function checkAndVibrate() { if (isUserInteracted) { navigator.vibrate(kickTime); } }

function startInterval() { if (!intervalId) { // Start the interval only if not already running intervalId = setInterval(checkAndVibrate, 1); // Interval duration is 1 millisecond } }

function stopInterval() { if (intervalId) { clearInterval(intervalId); intervalId = null; } }

// Start the interval after user interaction document.addEventListener("touchstart", startInterval);

// Stop the interval when necessary (e.g., on game exit) document.addEventListener("touchend", startInterval);

Maybe something like this should be included into the readme under the Android section? Again, it took Google Gemini to know a work-around to the restrictions.

Andalusio commented 6 months ago

Of course, the above code examples are ideal for working with intervals, which was what I was looking for for an in-game gimmick I had in mind.

jcesarmobile commented 6 months ago

You don't really need any of that code, you can still call vibrate even before the user touched the screen, it will just not vibrate and will return false