0

If have this code and it works like charm, but after the first result the in_array function only gives me "false". I cant find the Problem. Every time I restart the recognition, it works for one phrase...

var myArray = [
    'Kontakt', 
    'gehe zu Kontakt', 
    'navigiere zu Kontakt', 
    'navigiere zu Kontakte'
];

function in_array(needle, haystack, argStrict) 
{
    var key = '',
    strict = !! argStrict;
    if (strict) 
    {
        for (key in haystack) 
        {
            if (haystack[key] === needle) 
                return true;
        }
    } 
    else 
    {
        for (key in haystack) 
        {
            if (haystack[key] == needle)
                return true;
        }
    }

    return false;
};

recognition.onresult = function (event) 
{
    var pos = textArea.getCursorPosition() - interimResult.length;
    textArea.val(textArea.val().replace(interimResult, ''));
    interimResult = '';
    textArea.setCursorPosition(pos);
    for (var i = event.resultIndex; i < event.results.length; ++i) 
    {
        if (event.results[i].isFinal) 
        {
            insertAtCaret(textAreaID, event.results[i][0].transcript);
            speak = event.results[i][0].transcript;
            index = in_array(speak,myArray,false);
            alert(speak);
            alert(in_array(speak,myArray,false));
            if (index) 
            {
                alert(event.results[i][0].transcript);
            }
            else 
            {
                alert('Keine Übereinstimmung');
            }
        } 
        else 
        {
            isFinished = false;
            insertAtCaret(textAreaID, event.results[i][0].transcript + '\u200B');
            interimResult += event.results[i][0].transcript + '\u200B';
        }
    }
};

For testing a jsfiddle: http://jsfiddle.net/p3Fxc/

  • Far easier if you provide a jsfiddle – Lee Taylor May 23 '14 at 01:23
  • You shouldn't be returning from the middle of a function or loop construct like that, you should set a result variable, break the loop and always return at the end of the function. – scrowler May 23 '14 at 01:46

0 Answers0