我试图让我的代码与 responsiveVoice API 一起工作,但由于某种原因,它可以工作。它适用于“onclick”,但不适用于 if 语句。我究竟做错了什么?一切对我来说似乎都是正确的,但它不会起作用。代码如下所示。
function record() {
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.start();
recognition.onresult = function(event) {
let transcript = event.results[0][0].transcript;
var str = transcript;
if (str.includes('test')) {
responsiveVoice.speak('This works!');
}
document.getElementById('speechToText').value = event.results[0][0].transcript;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<label for="Speech Recognition">Speech Recognition</label>
<input type="text" name="" id="speechToText" placeholder="Speak Something" disabled="disabled">
<button onclick="record()">Record</button>
<p id="output"></p>
<script src='https://code.responsivevoice.org/responsivevoice.js'></script>
<script src="script.js"></script>
</body>
</html>
谢谢你的帮助。对此,我真的非常感激。