Timer function required (Javascript) - 120 secs to finish the quiz?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patelxxx
    New Member
    • May 2007
    • 135

    Timer function required (Javascript) - 120 secs to finish the quiz?

    I'm setting up a quiz where users have 120 secs to finish a quiz, what timer function do I use and how?

    After 120 secs I want to display a message, "Sorry timed out".

    Thank you

    patelxxx
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    heres an example of a clock, u can build on that.

    good luck

    Comment

    • patelxxx
      New Member
      • May 2007
      • 135

      #3
      thank you for that.

      cheers

      Comment

      • patelxxx
        New Member
        • May 2007
        • 135

        #4
        I've been studying the 'timer' script, however i don't understand some parts, please see my commented lines:


        [CODE=javascript]<script type="text/javascript">
        function startTime()
        {
        var today=new Date()
        var h=today.getHour s()
        var m=today.getMinu tes()
        var s=today.getSeco nds()
        // add a zero in front of numbers<10
        m=checkTime(m)
        s=checkTime(s)
        document.getEle mentById('txt') .innerHTML=h+": "+m+":"+s
        t=setTimeout('s tartTime()',500 ) //DON'T UNDERSTAND THIS
        }

        function checkTime(i) //DON'T UNDERSTAND THIS FUNCTION
        {
        if (i<10)
        {i="0" + i}
        return i
        }
        </script>[/CODE]

        Also, After the 120 sec timer limit, i want to disable the submit button, how is this done.

        cheers

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Line 12 uses setTimeout to call startTime() after 500 milliseconds which equals half a second. In this case, it might be better to use setInterval instead.

          The checkTime function just adds a zero if the minutes or seconds is one digit (less than 10). This gives a more uniform display, e.g. instead of 12:5:9, you'd see 12:05:09.

          Comment

          • patelxxx
            New Member
            • May 2007
            • 135

            #6
            and how would I disable the submit button once the 500 ms are over?

            cheers

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by patelxxx
              and how would I disable the submit button once the 500 ms are over?
              If your submit button looks like this:[HTML]<input type="submit" name="submit" id="submitbtn" value="Submit">[/HTML] then you can disable like this:[CODE=javascript]document.getEle mentById("submi tbtn").disabled =true;[/CODE]

              Comment

              • patelxxx
                New Member
                • May 2007
                • 135

                #8
                Ok cheers for that I have now got the disable button working via the setInterval() method and I can display the date too.

                However how can I 'count down' (and display this on screen) the setInterval ().

                Currently I have the setInterval() method to disable my submit button after two mins, however I wanted to display the 'count down' of the two mins on screen. So that it is easy for the user to see how long they have got left.

                cheers you have have been great help so far.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Show your code so far.

                  Comment

                  • patelxxx
                    New Member
                    • May 2007
                    • 135

                    #10
                    [CODE=javascript]
                    function disable_submit_ button() {
                    alert("Time Out");
                    get_results();
                    document.getEle mentById("submi t").disabled=tr ue;
                    }


                    function set_limit() {
                    var limit_ques = self.setInterva l("disable_subm it_button()", 60000);
                    alert("You have 1 minute to complete the questionnaire, PRESS OK TO BEGIN");


                    }[/CODE]

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Where's your countdown code? The clock code was in the right direction. You'll need two setIntervals. One for the disabling and the other for the countdown. Don't forget to clear the interval using clearInterval once you get to 0.

                      Comment

                      • patelxxx
                        New Member
                        • May 2007
                        • 135

                        #12
                        Cheers for that, all ok.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by patelxxx
                          Cheers for that, all ok.
                          Glad you got it all working. Post again anytime should you hit any more problems.

                          Comment

                          • hemantharjinder
                            New Member
                            • Sep 2011
                            • 1

                            #14
                            Heya patelxxx
                            plz can you show your full code here, i am a beginner and don't know much.

                            Comment

                            Working...