471,853 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

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

135 100+
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
Aug 24 '07 #1
13 10160
epots9
1,351 Expert 1GB
heres an example of a clock, u can build on that.

good luck
Aug 24 '07 #2
patelxxx
135 100+
thank you for that.

cheers
Aug 25 '07 #3
patelxxx
135 100+
I've been studying the 'timer' script, however i don't understand some parts, please see my commented lines:


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function startTime()
  3. {
  4. var today=new Date()
  5. var h=today.getHours()
  6. var m=today.getMinutes()
  7. var s=today.getSeconds()
  8. // add a zero in front of numbers<10
  9. m=checkTime(m)
  10. s=checkTime(s)
  11. document.getElementById('txt').innerHTML=h+":"+m+":"+s
  12. t=setTimeout('startTime()',500)       //DON'T UNDERSTAND THIS
  13. }
  14.  
  15. function checkTime(i)                 //DON'T UNDERSTAND THIS FUNCTION       
  16. {
  17. if (i<10) 
  18.   {i="0" + i}
  19.   return i
  20. }
  21. </script>
Also, After the 120 sec timer limit, i want to disable the submit button, how is this done.

cheers
Aug 25 '07 #4
acoder
16,027 Expert Mod 8TB
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.
Aug 26 '07 #5
patelxxx
135 100+
and how would I disable the submit button once the 500 ms are over?

cheers
Aug 26 '07 #6
acoder
16,027 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("submitbtn").disabled=true;
Aug 26 '07 #7
patelxxx
135 100+
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.
Aug 26 '07 #8
acoder
16,027 Expert Mod 8TB
Show your code so far.
Aug 27 '07 #9
patelxxx
135 100+
Expand|Select|Wrap|Line Numbers
  1. function disable_submit_button() {
  2.     alert("Time Out");
  3.     get_results();
  4.     document.getElementById("submit").disabled=true;
  5. }
  6.  
  7.  
  8. function set_limit() {
  9.     var limit_ques = self.setInterval("disable_submit_button()", 60000);
  10.     alert("You have 1 minute to complete the questionnaire, PRESS OK TO BEGIN"); 
  11.  
  12.  
  13. }
Aug 28 '07 #10
acoder
16,027 Expert Mod 8TB
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.
Aug 28 '07 #11
patelxxx
135 100+
Cheers for that, all ok.
Aug 30 '07 #12
acoder
16,027 Expert Mod 8TB
Cheers for that, all ok.
Glad you got it all working. Post again anytime should you hit any more problems.
Aug 30 '07 #13
Heya patelxxx
plz can you show your full code here, i am a beginner and don't know much.
www.jaipurcoaching.com
Sep 1 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Floortje | last post: by
2 posts views Thread by DaveF | last post: by
3 posts views Thread by nawhaley | last post: by
19 posts views Thread by arunkumar_m2001 | last post: by
1 post views Thread by jmgro | last post: by
2 posts views Thread by HeroinNO.4 | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.