<script language="javascript">
$.ajaxSetup({
async : false
});
let currentQuestionIndex = 0;
const questions = [];
// 检查浏览器是否支持语音合成
if ('speechSynthesis' in window) {
// 创建一个函数来朗读文本
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text.replace(/-/g, '减').replace(/=/g, '等于'));
speechSynthesis.speak(utterance);
}
// 修改displayQuestion函数,使其在显示问题的同时朗读问题
function displayQuestion(question,operator,fuhao) {
document.getElementById('question').textContent = question;
// 调用speak函数,将问题转换为语音
speak(operator + question + fuhao);
}
// 其他代码保持不变
} else {
console.error('当前浏览器不支持语音合成');
}
function generateQuestion() {
let result = 0;
const operators = ['+', '-'];
let question = '';
let lastOperator = '+'; // 初始操作符为加号
for (let i = 0; i < 3; i++) {
let number, operator;
let validNumber = false;
// 循环直到找到一个合适的数字
do {
number = Math.floor(Math.random() * 4) + 1; // 1-4的随机数
operator = operators[Math.floor(Math.random() * 2)];
// 检查当前操作符和数字是否会导致结果超出范围
if (operator === '+') {
validNumber = result + number <= 4;
} else if (operator === '-') {
validNumber = result - number >= 0;
}
} while (!validNumber);
lastOperator = operator; // 更新上一个操作符
setTimeout(() => {
if(i === 0)
{
displayQuestion(number,'','');
}
else if(operator === '+')
{
if(i === 2)
{
displayQuestion(number,'+','=');
}
else
{
displayQuestion(number,'+','');
}
}
else
{
if(i === 2)
{
displayQuestion(operator + number,'','=');
}
else
{
displayQuestion(operator + number,'','');
}
}
}, (i + 1) * 1000);//这里改为100,显示速度快了,但是语音速度还是很慢,没跟上,怎么让语音速度也变快
result += operator === '+' ? number : -number;
question += (i > 0 ? ' ' : '') + operator + number;
}
// 添加等号到问题字符串的最后
question += '=';
questions.push({ question: question, answer: result });
}
function checkAnswer() {
const userAnswer = parseInt(document.getElementById('answerInput').value);
if (userAnswer === questions[currentQuestionIndex].answer) {
alert('正确!');
} else {
alert('错误,正确答案是: ' + questions[currentQuestionIndex].answer);
}
document.getElementById('answerInput').value = '';
if (currentQuestionIndex < 2) {
currentQuestionIndex++;
document.getElementById('question').textContent = '';
generateQuestion();
} else {
alert('所有题目已完成!');
}
}
// 初始化第一题
generateQuestion();
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr class="STYLE2">
<td height="60" align="center" style="font-size: 50px; line-height: 60px; padding-top: 15px;"><div id="question"></div></td>
</tr>
<tr class="STYLE2">
<td height="150" align="center" style="font-size: 20px; line-height: 30px;"><input type="text" id="answerInput" placeholder="输入答案">
<button onclick="checkAnswer()">下一题</button></td>
</tr>
<tr class="STYLE2">
<td height="300" align="center">
</td>
</tr>
</table>
1000改为100,显示速度快了,但是语音速度还是很慢,没跟上,怎么让语音速度也一起变快