Add a Question Timer with Pause Button
Last updated
Last updated
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 following HTML element insider your question screen section--possibly at the bottom. This is the button to pause the game.
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.
Add the following function to your Javascript file at the bottom. This will change the pause state of the game and change the text of the button appropriately. Your HTML button runs this function on a click.
Add the following line to the top (but inside the curly braces) of your onClickedAnswer
function. This will hide the pause button after an answer is clicked. It probably doesn't make sense to have that button showing after the answer is clicked.
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.