javascript倒计时器,当用户没有提供答案并且命中零时,它会发布到服务器

时间:2016-07-21 14:44:15

标签: javascript html



<%@ page import="java.util.List" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Take Test</title>
    <div id="header">Java National Benchmark Test <strong>(NBT)</strong></div>
    <nav id="nav-3">
        <link rel="stylesheet" href="css/main.css">
        <a class="link-3" href="index.jsp">Home</a>
        <a class="link-3" href="about.jsp">About</a>
        <a class="link-3" href="register.jsp">Register</a>
        <a class="link-3" href="LogoutController">Logout</a>
    </nav>
    <script type="text/javascript" src="javascripts/selectAnswer.js"></script>
    <script language ="javascript" >
        var tim;
        var sec = 15;
        var f = new Date();
        function f2() {
            if (parseInt(sec) > 0) {
                sec = parseInt(sec) - 1;
                document.getElementById("showtime").innerHTML = "Time remaining: "+sec+" Secs";
                tim = setTimeout("f2()", 1000);
            }else {
                if (parseInt(sec) == 0) {
                    clearTimeout(tim);
                }
            }
        }
    </script>
</head>
<body onload="f2()" >
<div class="taketest">
<form id="TakeTestForm"  method="post" action="TakeTestController" onsubmit="return validateAnswer()">
    <div>
        <table width="100%" align="center">
            <tr>
                <td>
                    <div id="showtime" align="right"></div>
                </td>
            </tr>
            <tr>
                <td>
                </td>
            </tr>
        </table>
    </div>
    <%
        int questionsCounter = (Integer) session.getAttribute("questionsCounter");
        int totalSize = (Integer) session.getAttribute("totalSize");
    %>
    <p><font color="black">Question <%=questionsCounter%>/<%=totalSize%></font></p><br>
    <%
        String quest = (String) session.getAttribute("questName");
        List<String> options = (List<String>) session.getAttribute("optionsString");
    %>
    <fieldset>
        <legend><font color="black"><%=quest%></font></legend>
        <%
            for (int i = 0; i< options.size(); i++){
        %>
        <p>
            <input type="radio" style="color: dodgerblue" name="option" value="<%=options.get(i)%>"><%=options.get(i)%><br>
        </p>
        <%
            }
        %>
    </fieldset><br>
    <input type="submit" value="Next Question">
</form>
</div>
<div id="footer">
    <p align="center"><font color="white">Site developed by: Nizen Molobela <strong>(July 2016)</strong></font></p>
</div>
</body>
</html>
&#13;
&#13;
&#13;

如果用户的分配时间已过,我想转到下一个问题。我有一个定时测验应用程序,应该为用户提供时间,如果时间已经过期,它应该转到下一个问题。目前它确实显示倒计时器,但当它达到0时,它会停止并且没有任何反应。我希望它自动提交到服务器并转到下一个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

setTimeout(function(){ alert("You did not complete the question"); }, 3000);

这将在3秒(3000毫秒)后向用户发送消息。如果您的问题中没有任何代码,则很难给出具体答案,但您可以将其中的一个版本集成到您的解决方案中。

Further reading - W3Schools - Window TimeOut()