-2

I've written up a Trivia Game in Javascript, but am having a hard time understanding how to correctly implement a timer for each question. I've made it so that each question gets presented individually, and I'd like to have a timer for each question. If the timer runs out, it should proceed to the next question.

Here is a link to JSFiddle

If someone could take a few minutes and modify the JSFiddle, or let me know what I'd have to do in order to make each question count down from 10, I'd greatly appreciate it. Thanks a lot!

3
  • 1
    Putting code tags like link to get around not posting any code with a fiddle is not what you are supposed to do. You should post your code here, because if for some reason JSFiddle goes down this question is useless to the community. Also, we are not here to review your code. If you have an issue or a challenge post some code where you have attempted the problem at hand. Commented Jul 19, 2016 at 13:07
  • what you want is setTimeOut() nad/or setInterval(). w3schools.com/jsref/met_win_settimeout.asp w3schools.com/jsref/met_win_setinterval.asp Commented Jul 19, 2016 at 13:08
  • 1
    Thanks, @RAEC, I appreciate the links and help! Commented Jul 19, 2016 at 13:26

1 Answer 1

1

Timers in JavaScript work asynchronously. This is the first thing you should know. Secondly depending on what you need you can use either a:

  • setTimeout(function, timeout) This one allows you to delay an execution of a function provided by a time provided (in milliseconds).
  • setInterval(function, timer) This one makes the function call every timer milliseconds

Depending on how you intertwine these your code should do something like:

function timerExpired(){
  questionCounter++;
  newQuestion();
  setTimeout(timerExpired, 15000);
}
//This one will make sure that every 15 seconds, your questions get "moved on" to the next question. You can do the same with an interval, like so:
setInterval(function(){
  questionCounter++;
  newQuestion();
}, 15000);

This is as far as I can go without this turning into me writting your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Dellirium, that should get me somewhere! I'll let you know should I have another Q.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.