countdown timer in JSP/Servlets

2019-05-25 09:02发布

问题:

I am developing an online examination using servlets/jsp.I need to add a count down (hh/mm/ss) timer to the questions page that would end the exam and redirects to results page.

I am done with all the other functionalities except the timer one.

Can someone provide some help on this.

Thanks

回答1:

Have you tried a JavaScript solution. Its pretty easy to write your own. There is also a ton of examples on the net such as this one.



回答2:

<html>
<%@page session="false" %>
<%
HttpSession s=request.getSession(false);

if(s==null) { %>

    <jsp:forward page="/Expired" />
<% } %>
<% String duration=(String)s.getAttribute("duration"); %>
<% int a=Integer.parseInt(duration); %>
<head><title></title>
<script type="text/javascript">
var cmin=<%= a %>;
var total=cmin*60;
cmin=cmin-1;
var ctr=0;
var dom=document.getElementById("kulu");
function ram(){
var dom=document.getElementById("kulu");
dom.value=(cmin)+"minutes"+(total%60)+"seconds";
<% String t=(String)s.getAttribute("over"); %>
var tt=<%= t %>
if(tt=="false"){ram1();}
total=total-1;ctr++;
if(ctr==60){ctr=0;cmin=cmin-1;}
if(total==0){
ram1();}
setTimeout("ram()", 1000);
              }
function ram1(){

window.location.replace("/hcl/TTimeUp.jsp");

                }
</script>
</head>
<body background="image/background.gif" onload="ram()"><center>
<form name="form1">
<input type="text" id="kulu"/>
</form>
</center>
</body>
</html>