1

I have created a questionnaire where the user can answer the different questions in "text box". I tried to add a 30 second timer so that the user is forced to answer the different questions within the allotted time. But my timer is 30 seconds for ALL QUESTIONS, while I want it to be 30 seconds PER QUESTION. Can you help me do this please?

const questions = [
  { 
    question: 'hello',
    answers: 'hello',
    points: 1
  },
  { 
    question: 'cc',
    answers: 'cc',
    points: 1
  },
  { 
    question: 'hi',
    answers: 'hi',
    points: 1
  }
];

function *questionsGenerator( questions ) {
  yield* questions;
}

const questionaire = ( query, nextButton, target, onCompleted ) => {
  let score = 0;
  let iterator = questionsGenerator( query );
  let question = null;
  let selectedAnswer = -1;
  let minuteur = 30;
  
  nextButton.addEventListener('click', nextQuestion);
  
  function evaluateAnswer() {

    if (!question) {
      return;
    }
    if (selectedAnswer < 0) {
      return;
    }
    if (question.answers === selectedAnswer) {
      score += question.points;
    }
    return;
  }

  function decrementMinuteur(){
    minuteur--
  }
  
  function nextQuestion() {
    evaluateAnswer();
    question = iterator.next();
    if (question.done) {
      nextButton.removeEventListener('click', nextQuestion);
      onCompleted( score );
      return;
    }
    question = question.value;
    drawUI();
  }

  const timer = document.createElement('span');
  timer.setAttribute("id", "timer");
    document.body.appendChild(timer)
  function drawUI() {
    nextButton.setAttribute('disabled', true);
    selectedAnswer = -1;
    Array.from( target.childNodes ).forEach( child => target.removeChild( child ) );
    const title = document.createElement('h1');
    title.innerHTML = question.question;
    target.appendChild( title );
    
    const timer = document.getElementById("timer");
    let id = setInterval(()=>{
        if(minuteur>0){
          decrementMinuteur();
          timer.textContent = minuteur;
        }
    },1000)

      const el = document.createElement('input');
      el.type = 'text';
      el.name = 'answer';
      el.value = '';
      el.id = 'answer';
      el.addEventListener('change', () => { 
        selectedAnswer = el.value.trim(); 
     
      } );
      el.addEventListener('keydown', () => { 
        nextButton.removeAttribute('disabled'); 
      } );
            
      const container = document.createElement('div');
      container.appendChild(el);
      
     
      target.appendChild(container);
      el.focus();
  }
  
  return {
    nextQuestion
  }
};


questionaire(
  questions, 
  document.querySelector('#btnNext'), 
  document.querySelector('#questionaire'), 
  score => alert('You scored ' + score ) 
).nextQuestion();

This is related to me earlier question Create a questionnaire Javascript with "text box"

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 2
    You could reset `minuteur` back to 30 when you're done evaluating the answer – D.Schaller Nov 16 '21 at 08:41
  • Thanks, but how do I do this? Like this is doesn't work : function evaluateAnswer() { ....... } let minuteur = 30; function decrementMinuteur(){ minuteur-- } – PitxRosalina Pitalina Nov 16 '21 at 08:51
  • 1
    I'd set it within the `evaluateAnswer()` or the `nextQuestion()` e.g.: `if(question.done) { minuteur = 30; [...] }` – D.Schaller Nov 16 '21 at 09:08
  • I have add minuteur=30; in the nextQuestion() function but nothing changes :/ The timer still does not display 30 seconds per question. It's still 30 seconds for all questions :/ function nextQuestion() { evaluateAnswer(); question = iterator.next(); if (question.done) { minuteur = 30; ........ } – PitxRosalina Pitalina Nov 16 '21 at 09:15
  • My index.html if you want test :

    questionnaire

    – PitxRosalina Pitalina Nov 16 '21 at 09:25
  • Please provide a [mwe](https://stackoverflow.com/help/minimal-reproducible-example). Your issue could likely be solved by either using [debouncing](https://stackoverflow.com/questions/24004791/can-someone-explain-the-debounce-function-in-javascript) or just a global timer that is reset when desired. – async await Nov 17 '21 at 23:21

0 Answers0