<script>
....省略
function generateQuestion() {
let result = 0;
const operators = ['+', '-'];
let question = '';
let lastOperator = '+'; // 初始操作符为加号
let lastNumber = null; // 初始化上一个数字为null
let buttonsDisabled = true; // 按钮初始状态为不可点击
if(type==7)
{
if(bishu==2)
{
bishu=3;
}
}
for (let i = 0; i < bishu; i++) {//几笔
let number, operator;
let validNumber = false;
let result_onesDigit=onesDigit(result);
// 循环直到找到一个合适的数字
do {
if(type==1)//1-4直加直减
{
number = Math.floor(Math.random() * 4) + 1; // 1-4的随机数
operator = operators[Math.floor(Math.random() * 2)];
// 检查当前操作符和数字是否会导致结果超出范围
if (operator === '+') {
validNumber = result + number <= 4;
}
else
{
validNumber = result - number >= 0;
}
}
// 检查新数字和操作符与上一个数字和操作符是否不同
if (lastNumber !== null && (number === lastNumber || (lastOperator === operator && number === lastNumber))) {
validNumber = false;
}
} while (!validNumber);
lastOperator = operator; // 更新上一个操作符
lastNumber = number; // 更新上一个数字
setTimeout(() => {
if(i === 0)
{
displayQuestion(number,'','');
}
}, (i + 1) * shijian*1000);//间隔时间*1000
result += operator === '+' ? number : -number;
question += (i > 0 ? ' ' : '') + operator + number;
}
questions.push({ question: question, answer: result });
}
function checkAnswer() {
if($.trim($("input[name=answerInput]").val()) == "")
{
$("input[name=answerInput]").focus();
return false;
}
document.getElementById('question1').textContent='';
isQuestionDisplayed = false;//每一题重新禁用按纽
const userAnswer = parseInt(document.getElementById('answerInput').value);
if (userAnswer === questions[currentQuestionIndex].answer) {
alert('正确!');
} else {
alert('错误,正确答案是: ' + questions[currentQuestionIndex].answer);
}
document.getElementById('answerInput').value = '';
$("input[name=answerInput]").focus();
if (userAnswer !== undefined) { // 确保 userAnswer 已被定义
questions[currentQuestionIndex].userAnswer = userAnswer; // 添加这一行
}
if (userAnswer === questions[currentQuestionIndex].answer) {
questions[currentQuestionIndex].isCorrect = true;
} else {
questions[currentQuestionIndex].isCorrect = false;
}
if(tiliang==1)
{
if (currentQuestionIndex < zushu-1) {//几道题
currentQuestionIndex++;
document.getElementById('question').textContent = '';
generateQuestion();
} else {
localStorage.setItem('questionsData', JSON.stringify(questions));
window.location.href = "/oa/oa_zxsxl.php?act=jieguo";
}
}
}
省略......
</script>
怎么把全部题做完用了多少时间也统计到新页面/oa/oa_zxsxl.php?act=jieguo中去?