This is an example of adding a timer to the question screen that counts down to zero.
Add the following HTML element inside your question screen section--and probably at the top of the screen. This will be where your timer digits will be displayed.
Add the code shown below to your displayQuestion ( )
function inside the curly braces { }
of the function's code block. The Javascript function called setInterval
runs a block of code at a set interval. In this example, we run a block of code every 100 milliseconds that allows us to update the countdown timer accurately. If the timer reaches zero, it triggers an incorrect answer using trivia.triggerAnswer(false)
. Notice that you can adjust the time limit by changing the timeLimit
variable.
Below is an example of some basic CSS properties you could apply in your stylesheet. Adjust as needed.
That's it. You should have a functioning timer that you can modify as you wish.