Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Does clearTimeout actually do anything?

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?

Best Answer
0 Votes
1 REPLY 1

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");
Best Answer
0 Votes