how to stable count down timer!!
hey guys!!
i am doing an online project in jsp + javascript!
its an online examination project where i m using countdown timer.
i am taking each time one question from database which is in DB2.
timer is working perfect but whenevr i load the same page again timer code gets execute again n start from its initial point.
is thr any idea to stop it?? i mean i only need to execute timer once when i m loading the page i am loading every question again and again on same page.
i m newbie in javascript so i dont have much idea how to control these things in javascript. the remaining page is written in jsp. like database connectivity n retrieval of data from database.TIMER code is written in javascript. i m putting the sample of the code here so u guys can make out wht i need to stop it whn i will again load the same page for retriving anothe rquestion from database .
please help me out with any possible solution available.
here the code is
<HTML>
<HEAD>
<TITLE>Timer</TITLE>
</HEAD>
<%
int id = 0;
String parm=request.getParameter("id");
if (parm != null)
id = Integer.parseInt(parm);
%>
<BODY onload="startTimer()">
<DIV ALIGN="center">
Countdown Timer
<FORM>
Time Elapsed: <br>
<INPUT TYPE="text" NAME="timeLapse" VALUE=""><BR>
Time Remaining: <br>
<INPUT TYPE="text" NAME="timerDisplay" VALUE=""><BR>
</FORM>
</DIV>
<SCRIPT LANGUAGE="JavaScript">
<!--
//var running = false;
var running;
var endTime = null;
var timerID = null;
var theTime = null;
var minutes = null;
var seconds = null;
var start = null;
// var start;
document.bgColor = "#FAFAD2";
function startTimer() {
running = true;
var now = new Date();
start =now.getTime();
//var minutes = document.forms[0].minutes.value;
//minutes = lapse.getMinutes();
//seconds = lapse.getSeconds() + 1;
//var seconds = document.forms[0].seconds.value;
var setTime = 1000 * 60 * minutes + 1000 * seconds;
endTime = start + setTime;
showCountDown()
}
function showCountDown() {
document.bgColor = "#7FFFD4";
var present = new Date();
moment = present.getTime();
if (endTime - moment<= 0) {
stopTimer();
document.forms[0].timerDisplay.value = "TIME'S UP!";
turnRed();
move();
} else {
var lapse = new Date(moment - start);
var minLapse = lapse.getMinutes();
var secLapse = lapse.getSeconds() + 1;
var timeLapse = minLapse;
timeLapse += ((secLapse < 10) ? ":0" : ":") + secLapse;
document.forms[0].timeLapse.value = timeLapse;
var delta = new Date(endTime - moment);
var theMin = delta.getMinutes();
//minutes = delta.getMinutes();
var theSec = delta.getSeconds();
//seconds = delta.getSeconds();
theTime = theMin;
theTime += ((theSec < 10) ? ":0" : ":") + theSec;
document.forms[0].timerDisplay.value = theTime;
if (running) {
timerID = setTimeout("showCountDown()",1000)
}
}
}
function stopTimer() {
document.forms[0].timerDisplay.value = theTime + " TIMER STOPPED";
clearTimeout(timerID);
running = false;
setTimeout("document.bgColor = '#FAFAD2'", 3000);
}
function turnRed() {
document.bgColor = "#000000";
setTimeout("document.bgColor = '#FF0000'", 500);
}
function move() {
//alert ("TIME'S UP");
window.location = 'http://localhost:8080/Online/login.jsp'
// alert ("TIME'S UP");
}
// -->
</SCRIPT>
Click <a href="fin_time.jsp?id=<%=id+1 %>">For Next Question</a>
<BR><BR>
</BODY>
</HTML>
Krishna
|