jQuery練習2——倒計時

最近學生作一個在線考試系統,須要加入試卷倒計時功能。很顯然最放方便的就是ajax實現。一會兒想起上次提到的jQuery,一用果真簡單。jQuery中提供了ajax的方便功能。
技術:HTML+Servlet+jQuery
實現功能:頁面顯示倒計時,時間來自服務器。到時間後自動交卷。
Servlet代碼以下:
response.setContentType( "text/html;charset=UTF-8");
response.setHeader( "Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
try {
         if ( "submit".equals(request.getParameter( "action"))) {
                 //交卷
                 //TODO:這裏作交卷處理
                out.println( "已交卷");
        } else {
                 //倒計時
                 //設置結束時間
                Calendar timeend = Calendar.getInstance();
                timeend.set(2008, 10, 7, 14, 50, 0);
                 long end = timeend.getTimeInMillis();
                 //out.print(sdf.format(timeend.getTime())+"<br>");
                 //獲取當前時間
                Calendar rightNow = Calendar.getInstance();
                 //out.print(sdf.format(rightNow.getTime())+"<br>");
                 long now = rightNow.getTimeInMillis();
                 //計算剩餘時間
                 int left = ( int) (end - now);
                 if (left <= 0) {
                         //時間到
                        out.print( "over");
                } else {
                         int leftHour = left / (1000 * 60 * 60);
                        left = left % (1000 * 60 * 60);
                         int leftMinute = left / (1000 * 60);
                        left = left % (1000 * 60);
                         int leftSecond = left / (1000);
                        out.print(leftHour + "時" + leftMinute + "分" + leftSecond + "秒");
                }
        }
} finally {
        out.close();
}
HTML就交給jQuery了,代碼以下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
         < head >
                 < title > </title>
                 < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
                 < script src ="../js/jquery.js" type ="text/javascript" > </script>
                 < script type ="text/JavaScript" >
                        $(document).ready(function(){
                                setInterval(getLeftTime,1000);
                        });
                        //獲得剩餘時間
                        function getLeftTime(){
                                $.get("/jquery/TimeServlet?",function(data){
                                        if(data=="over"){
                                                //window.location="../index.jsp";
                                                $("#paperform").submit();
                                        }
                                        else{
                                                $("#nowis").text(data);
                                        }
                                });
                        }
                 </script>
         </head>
         < body >
                 < div id ="nowis" > </div>
                 < form id ="paperform" method ="POST" action ="/jquery/TimeServlet?action=submit" >
                 </form>
         </body>
</html>
測試。
唉,jQuery命名就是一個JavaScript庫,說的人多了,居然也成了一門技術的名稱。呵呵。
相關文章
相關標籤/搜索