Web Speech API in_array

时间:2014-05-23 00:32:14

标签: javascript arrays speech webspeech-api

如果有这个代码,它就像魅力,但在第一个结果后,in_array函数只给我“假”。我找不到问题。每次重新开始识别时,它都适用于一个短语......

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';
        }
    }
};

测试jsfiddle: http://jsfiddle.net/p3Fxc/

0 个答案:

没有答案