0

const resumeInfinity = () => {
  window.speechSynthesis.resume();
  const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
  console.log(timeoutResumeInfinity);
}
      
utterThis.onstart = () => {
 resumeInfinity();
};

need the (resumeInfinity) function to stop working after the speechSynthesi

t.niese
  • 39,256
  • 9
  • 74
  • 101
Rukkiecodes
  • 33
  • 1
  • 7

1 Answers1

0

To avoid error messages that resumeInfinity is not a function you shouldn't delete it but set it to an empty function. But as you set defined resumeInfinity as const you can't change that function.

So what you can do is to either change it to:

let resumeInfinity = () => {
   // ... your code
}

And then do change it later to an empty function resumeInfinity = () => {} But you need to keep in mind that if that original function was passed as callback earlier to somewhere else (e.g. like here setTimeout(resumeInfinity, 1000)) that this callback will still refer to the old function.

So a better solution would be to check if the function should still valid to be executed and do an early out otherwise.

const resumeInfinity = () => {
  if( /* some check if isn't valid to call that function anymore */ ) {
     return
  }
  window.speechSynthesis.resume();
  const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
  console.log(timeoutResumeInfinity);
}

But all of these above solutions are in fact just a workaround, because if the logic of your application is correct then such a situation should never happen. So the need to do something like this indicates that you more likely need to think about restructuring your code.

If it is just about stopping the timeout then you need to call clearTimeout(timeoutResumeInfinity), and make timeoutResumeInfinity available at the place at which you know that the speechSynthesi finished.

t.niese
  • 39,256
  • 9
  • 74
  • 101