Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts

    creating a quiz using javascript as the timer

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This should start you off:-

    Code:
    <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;
    n--;
    if(n >=0) {
    document.getElementById( 'time').value = "Answer in " + n + " seconds";
    }
    else {
    clearInterval(i);
    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) {
    clearInterval(i);
    alert("Right Answer with " + n + " seconds remaining");
    }
    }
    }
    </script>
    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.
    Last edited by Philip M; 09-12-2008 at 07:40 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    LinkBacks (?)

    1. 03-21-2014, 07:22 PM

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •