中断和复位设置超时 - Javascript

Interrupt and Reset set Timeout - Javascript

我有以下问题:我目前正在开发一个应该模拟考试情况的聊天机器人。因此我让它提问,用户应该有 x 时间来回答问题。一旦时间到了,机器人应该问下一个问题。但是,一旦学生发送了答案(按回车键 -> 因为这是继续对话的唯一方法)机器人应该中断并重置计时器并提出新问题。

我有以下概念

它似乎工作了一次 - 所以基本上是从问题 1 跳到问题 2 但在另一个 Enter 事件之后,它 returns 接下来的两个问题等等。

我没看错什么?

const Question1E = " 1-This is the easy Question";
const Question1H = " 1-This is the hard Question";
const Question2E = " 2-This is the easy Question";
const Question2H = " 2-This is the hard Question";
const Question3E = " 3-This is the easy Question";
const Question3H = " 3-This is the hard Question";
const Question4E = " 4-This is the easy Question";
const Question4H = " 4-This is the hard Question";
const Question5E = " 5-This is the easy Question";
const Question5H = " 5-This is the hard Question";
const Question6E = " 6-This is the easy Question";
const Question6H = " 6-This is the hard Question";
const Done = " DONE ";

let count = 1;
let question = null;
const scores = [];

const questions = {
  1: {
    intent: "Intent1",
    easy: Question1E,
    hard: Question1H,
  },
  2: {
    intent: "Intent2",
    easy: Question2E,
    hard: Question2H,
  },
  3: {
    intent: "Intent3",
    easy: Question3E,
    hard: Question3H,
  },
  4: {
    intent: "Intent4",
    easy: Question4E,
    hard: Question4H,
  },
  5: {
    intent: "Intent5",
    easy: Question5E,
    hard: Question5H,
  },
  6: {
    intent: "Intent6",
    easy: Question6E,
    hard: Question6H,
  },
};

function test() {
  const timer = setTimeout(() => {
    test();
  }, 10000);

  const enter = document.addEventListener("keydown", function(e) {
    if (e.key === "Enter") {
      clearTimeout(timer);
      test();
    }
  });

  if (count < 6) {
    const currentquestion = questions[count];
    //IGNORE THIS PART
    if (count <= 1) {
      let randomizer = Math.random();
      randomizer <= 0.5 ?
        (question = currentquestion.easy) :
        (question = currentquestion.hard);
    } else {
      scores[scores.length - 1] >= 0.65 ?
        (question = currentquestion.hard) :
        (question = currentquestion.easy);
    }
    //CONTINUE HERE
    console.log(`${question}`);
  } else {
    clearTimeout(timer);
    console.log(`${Done}`);
  }
  count++;
}

test();

  1. 定时器需要在函数外
  2. test 需要是它自己的函数

let timer;
const test = () => {

  if (count < 6) {
    const currentquestion = questions[count];
    //IGNORE THIS PART
    if (count <= 1) {
      let randomizer = Math.random();
      randomizer <= 0.5 ?
        (question = currentquestion.easy) :
        (question = currentquestion.hard);
    } else {
      scores[scores.length - 1] >= 0.65 ?
        (question = currentquestion.hard) :
        (question = currentquestion.easy);
    }
    //CONTINUE HERE
    console.log(`${question}`);
    timer = setTimeout(test, 2000); // set it to something shorter while testing  

  } else {
    clearTimeout(timer);
    console.log(Done);
  }
  count++;
};


const enter = document.addEventListener("keydown", function(e) {
  if (e.key === "Enter") {
    clearTimeout(timer);
    test()
  }
});
test(); // start
<script>
  // moved the setup
  const Question1E = " 1-This is the easy Question";
  const Question1H = " 1-This is the hard Question";
  const Question2E = " 2-This is the easy Question";
  const Question2H = " 2-This is the hard Question";
  const Question3E = " 3-This is the easy Question";
  const Question3H = " 3-This is the hard Question";
  const Question4E = " 4-This is the easy Question";
  const Question4H = " 4-This is the hard Question";
  const Question5E = " 5-This is the easy Question";
  const Question5H = " 5-This is the hard Question";
  const Question6E = " 6-This is the easy Question";
  const Question6H = " 6-This is the hard Question";
  const Done = " DONE ";

  let count = 1;
  let question = null;
  const scores = [];

  const questions = {
    1: {
      intent: "Intent1",
      easy: Question1E,
      hard: Question1H,
    },
    2: {
      intent: "Intent2",
      easy: Question2E,
      hard: Question2H,
    },
    3: {
      intent: "Intent3",
      easy: Question3E,
      hard: Question3H,
    },
    4: {
      intent: "Intent4",
      easy: Question4E,
      hard: Question4H,
    },
    5: {
      intent: "Intent5",
      easy: Question5E,
      hard: Question5H,
    },
    6: {
      intent: "Intent6",
      easy: Question6E,
      hard: Question6H,
    },
  };
</script>