0

This JavaScript code is a small part of Web Speech API to implement Speech Recognition. "recognition.onend function" will be fired Whenever speech recognition service has disconnected, for example when "recognition.stop()" happens. Now The problem is because "recognition.stop()" happens in my if statement each time "recognition.onend function" fires again and it makes a loop over and over.

How To Stop this loop?

recognition.onend = function(event) {
    //Fired when the speech recognition service has disconnected.
    if (speechResult === "Hello") {
        recognition.stop();
    } else {
        recognition.stop();
        // and do something else
    }
};
ingvar
  • 4,169
  • 4
  • 16
  • 29
  • 1
    If it's already in process of ending, do you need to `stop()` it at all? Won't it do that already? – ceejayoz Nov 08 '18 at 21:07
  • Maybe pass null to recognition.onend, so there will be no function to call? – sawim Nov 08 '18 at 21:08
  • So why do you trigger `onend` in your `stop` function? You should make up your mind whether you call `onend` in `stop` or vice versa, but not both. – trincot Nov 08 '18 at 21:08
  • Is there a recognition.state or recognition.started property you can check to see what's going on? Otherwise, pair recognition with a state variable and set it to true when it's started and false when you stop it. Use that to determine what you do in the event handler. But, probably, you've fallen into a pit of failure and should be manipulating the api differently, but I can't give you hints how. –  Nov 08 '18 at 21:08

0 Answers0