0

I want to be able to set a function onbroadcast in SpeechRecognition after I create a new SpeechRecognition object so that I can call this function internally if certain conditions are met.

I would like to be able to set it in the same way that you would set something like onerror in webkitSpeechRecognition. When I look at onerror in the Developer Tools it looks like it might be done via some sort of getter/setter like what is described here but I can't be certain.

Is this possible?

recognition.js:

var SpeechRecognition = function () {
    var recognitionObject = new webkitSpeechRecognition();
    recognitionObject.onresult = function (event) {
        if(event.results.length > 0) {
            if (onbroadcast !== null && onbroadcast === 'function') {
                onbroadcast('there are results');
            }
        }
    }
    recognitionObject.onerror = function (event) {
        console.log(event);
    }
    recognitionObject.onend = function (event) {
        console.log(event);
    }
    recognitionObject.start();
}

SpeechRecognition.prototype.onbroadcast = null;

main.js:

var sr = new SpeechRecognition();
sr.onbroadcast = function(msg) {
    document.getElementById('id') = msg;
}
millarnui
  • 549
  • 1
  • 6
  • 17
  • `onupdateraw` and `onbroadcast` are undeclared variables, what did you mean to do with them? I suppose you want to refer to a `.onbroadcast` **property** of your `SpeechRecognition` instance - typically with `this` [but not in a callback](http://stackoverflow.com/q/20279484/1048572) like here. – Bergi Jun 15 '16 at 22:58
  • Sorry - `onupdateraw` was a typo. – millarnui Jun 15 '16 at 23:05
  • I want to set `onbroadcast` from outside of the object somehow, but call it from within (if it is set) – millarnui Jun 15 '16 at 23:06
  • @millarnui what do you mean by `sr.onbroadcast = function(msg) { document.getElementById('id') = msg; }` ?! – Bekim Bacaj Jun 16 '16 at 00:52

1 Answers1

0

You need to refer to onbroadcast as a property of your instance (this.onbroadcast). It doesn't magically become available as a variable inside the constructor scope.

function SpeechRecognition() {
    var self = this; // a reference to the instance
    var recognitionObject = new webkitSpeechRecognition();
    recognitionObject.onresult = function (event) {
        if (event.results.length > 0 && typeof self.onbroadcast === 'function') {
            self.onbroadcast('there are results');
//              ^ a property access
        }
    };
    recognitionObject.onerror = recognitionObject.onend = function (event) {
        console.log(event);
    };
    recognitionObject.start();
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • "self" is a reserved word @Bergi – Bekim Bacaj Jun 15 '16 at 23:37
  • Immediately after I set `sr.broadcast` in main.js I can see that the property has been set but when `recognitionObject.onresult` is fired `self.onbroadcast` === undefined. – millarnui Jun 15 '16 at 23:38
  • @BekimBacaj: No it's not. It's a global variable (`window.self`), yes, but not reserved. – Bergi Jun 15 '16 at 23:42
  • @millarnui: I cannot reproduce with my code. Notice that in your OP you also forgot the `typeof` keyword – Bergi Jun 15 '16 at 23:44
  • @millarnui: You might want to put a `console.log(self)` in the `onresult` handler – Bergi Jun 15 '16 at 23:44
  • @BekimBacaj: I'm not! It's just a local variable inside the scope of the constructor. You could've named it anything. – Bergi Jun 16 '16 at 00:51