05-22-2018 08:23 - edited 05-22-2018 08:23
05-22-2018 08:23 - edited 05-22-2018 08:23
Using setTimeout to trigger a vibration repeat and then a clearTimeout to end it.
function startAlertProcess(message) {
showAlert(message);
startVibration("ping");
vibrationTimeout = setTimeout(function(){ startVibration("ping"); console.log("triggered vibe by setTimeout"); }, 10000);
}
function startVibration(type) {
vibration.start(type);
}
function stopVibration() {
clearTimeout(vibrationTimeout);
vibration.stop();
}
Now triggered by a button press stopVibration should cancel the vibration, and to my mind cancel it immediately. But whenever startAlertProcess is called and the stopVibration called there is always an additional vibration after the clearTimeout is executed which leads me to wonder if the clearTimeout actually does anything.
Other arrangements had a different triggering process but that led to the vibration never stopping leading me to this question, or am I really just using clearTimeout wrong?
06-05-2018 16:26
06-05-2018 16:26
Seems to work for me:
import document from "document"; import { vibration } from "haptics"; let vibrationTimeout; function startAlertProcess(message) { //showAlert(message); console.log("start alert"); startVibration("ping"); vibrationTimeout = setTimeout(function(){ startVibration("ping"); console.log("triggered vibe by setTimeout"); }, 10000); } function startVibration(type) { console.log("start vibe") vibration.start(type); } function stopVibration() { clearTimeout(vibrationTimeout); vibration.stop(); } document.onkeypress = function() { console.log("anykey"); stopVibration(); } startAlertProcess("let's go");