View Full Version : creating a quiz using javascript as the timer

09-12-2008, 07:59 PM
I am at the first stage of creating a quiz.

what i wish to know is how i can limit the time taken between each questions. preventing the questions being looked up!

quick run down on the method i want to use...

0) create an empty entry for the users answers

1) questions to be asked using PHP's for/next loop (10 questions)

2) questions taken from mysql databases (for simplicity i will only have two possible answers)

(now this is the part i need help with....)
3) submit button shown for user to click to start quiz. (ten second timer started)

4) if answer selected, store the answer in the user answers entry in mysql

5) if no answer selected in ten seconds, then (next) go to number (1) above, to show next question after the user clicks the button to show next question

i will simplify this a lot more i am sure later on. but just to give an idea on the method i want to use.

the bit i need help with when i have written this is the timer part.

how do i limit the time they have to answer each question.
been told to use javascript but no idea, never used it before and really would appreciate some help

thanks in advance.

Philip M
09-12-2008, 08:33 PM
This should start you off:-

<input type = "text" id = "answer">
<input type = "button" id = "time" onclick="answer()">

<script type = "text/javascript">
var n = 10; // modify this for number of seconds to answer
document.getElementById( 'time').value = "Answer in " + n + " seconds";
var i = setInterval('count()' , 1000);
var tooLate;

function count() {
tooLate = 0;
if(n >=0) {
document.getElementById( 'time').value = "Answer in " + n + " seconds";
else {
alert("Too late!");
document.getElementById('answer').value = "";
tooLate = 1;

function answer() {

var correct = "CORRECT"; // This is the correct answer
if (tooLate == 0) {
if(document.getElementById( 'answer').value == correct) {
alert("Right Answer with " + n + " seconds remaining");

You will need to create an array variable containing the correct answers to the ten questions.

BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

09-12-2008, 09:56 PM
i have taken a look at the code but i am very new to javascript.

i sort of understand the functions but nothing happen when i visited the page i placed it in.

i made a start on the php for the quiz and came up with many problems so think i may have to think about using a ready made one, can anyone suggest a quiz script with a timed part to limit the amount of time users take on answering the questions.

also one if possible that will ask the user to click a button to proceed to the next question.