-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!

swedishcheef
  • 549
  • 2
  • 11
  • 25
  • 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. – Adjit Jul 19 '16 at 13:07
  • what you want is setTimeOut() nad/or setInterval(). http://www.w3schools.com/jsref/met_win_settimeout.asp http://www.w3schools.com/jsref/met_win_setinterval.asp – RAEC Jul 19 '16 at 13:08
  • 1
    Thanks, @RAEC, I appreciate the links and help! – swedishcheef Jul 19 '16 at 13:26

1 Answers1

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.

Dellirium
  • 1,362
  • 16
  • 30