Solved

timer count coldfusion

Posted on 2012-03-10
10
630 Views
Last Modified: 2012-03-16
hi,
i'm developing an online quiz application in cold fusion and want a timer in quiz page. The time value will come from a table. so if quiz time is 1 hour 30 minutes, it will show the 1 hours, 30 minutes and seconds running on quiz page. or if time is  15 minutes then it will show 0 hours 15 minutes and seconds. it will also prompt an alert before ending the timer in last 1 or 2 minute. and so as soon as the times out the quiz page will redirect to results page.  Timer would be in real time like 01:30:23.
hope i've explained it in detail. any help, comments or feedback would be deeply appreciated.
0
Comment
Question by:s_hausen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37705480
What you can do is

1. get the timer in seconds from the table and store it in a local javascript variable.

2. Have a method that will display the timer value formatted as required

3. There is a setTimeOut method in javascript that can be used to call call a method every second that will reduce the number of seconds left by one and call the display method again

4. In the same method if the number of seconds left is 60 you can give the alert

5. In the same method if the number of seconds left is 0, you forward to the result page.

Hope this is what you were looking for
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37705567
In addition, you should revalidate the time elapsed when the quiz is submitted. Otherwise a user could take as much time as they want by disabling javascript. One approach might be to store an encrypted start time in a form field. Then use it to validate the time elapsed on submission. Or you could log the start times in a db table. Just do not embed the information in plain text or it can be spoofed.

I'd also suggest using a jquery plugin for the timer instead of rolling your own. I haven't used it but this one looks nice.
0
 

Author Comment

by:s_hausen
ID: 37705733
i'm not very good with javascript and looking for a sample code whch is doing the same function as i'm desiring. Also i've one more issue, which is when user hit submit for each questions form values go to answersubmit page and then redirect back to quiz page. does it will effect the timing code on the quiz page??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 total points
ID: 37707420
Check this code....

<HTML>


<SCRIPT>

var maxTime;
var timeLeft;
var alertwhen = 10; //How many seconds early the alert needs to be given

function startTimer(){

	maxTime = document.timerForm.maxTime.value;
	timeLeft = eval(maxTime);
	setTimeout("checkTimer()",1000);

}


function checkTimer(){

	if(timeLeft == alertwhen){
		alert('Hurry, Only '+alertwhen+' seconds left!!!');
	}

	if(timeLeft == 0 ) {
		//Submit the form to the next page
		alert('Time UP!!!');
	} else {

		timeLeft--;
		document.getElementById('timeLeft').innerHTML = timeLeft;
		setTimeout("checkTimer()",1000);
	}

}


</SCRIPT>

<BODY onload="javascript:startTimer();" >
	<form name="timerForm">
	<input type="hidden" name="maxTime" value ="30" />

	Time Left : <SPAN id="timeLeft"></SPAN>
	</form>
</BODY>

</HTML>

Open in new window


Also as _agx_ suggested you need to do a double check at the server side.
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 37708870
ok my 2 cents, what if i though the keyboard use the ctrol+o, it will open the file popup and ur timeer will stop

;)
0
 

Author Comment

by:s_hausen
ID: 37711143
I do appreciate everyone's help, comments and feedback on this question, but unfortunately not getting the solution of my question. So I guess i better go ahead and try to write my own javascript and will post it on this thread. might take a day or two :)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37711165
Sorry for the delay in responding... I've got a deadline to meet and can't work up an example until tomorrow  (if nobody else has answered by then)
0
 

Author Comment

by:s_hausen
ID: 37711808
thx _agx, i'm also trying to solve this puzzle. Looking forward for your response.
0
 

Author Comment

by:s_hausen
ID: 37731428
finally i got the solution:

<script type="text/javascript">  
  var hours = 0  
  var minutes = 30
  var seconds = 10
function setCountDown ()
{
  seconds--;
  if (seconds < 0){
      minutes--;
      seconds = 59
  }
  if (minutes < 0){
      hours--;
      minutes = 59
  }
  if (hours < 0){
      hours = 23
  }
  document.getElementById("remain").innerHTML = hours+" Hours "+minutes+" Minutes "+seconds+" Seconds";
  SD=window.setTimeout( "setCountDown()", 1000 );
  if (hours == '00' && minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
               window.alert("Time is up. Press OK to continue.");
       }

}
</script>

<div id="remain"></div>
0
 

Author Closing Comment

by:s_hausen
ID: 37731432
code helped me to solve the main problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question