(Abstract) jquery Exercise 2-countdown

Source: Internet
Author: User
Code:
  1. Recently, an online examination system needs to be added to the examination countdown function. Obviously, Ajax is the most convenient implementation. It's easy to use jquery. Jquery provides convenient Ajax functions.
  2. Technology: HTML + servlet + jquery
  3. Function: Countdown is displayed on the page. The time comes from the server. The volume is automatically handed in after the time.
  4. The servlet code is as follows:
  5. Response. setcontenttype ("text/html; charset = UTF-8 ");
  6. Response. setheader ("cache-control", "No-Cache ");
  7. Printwriter out = response. getwriter ();
  8. Try {
  9. If ("Submit". Equals (request. getparameter ("action "))){
  10. // Submit the paper
  11. // Todo: Submit the volume here
  12. Out. println ("submitted ");
  13. } Else {
  14. // Countdown
  15. // Set the end time
  16. Calendar timeend = calendar. getinstance ();
  17. Timeend. Set (2008, 10, 7, 14, 50, 0 );
  18. Long end = timeend. gettimeinmillis ();
  19. // Out. Print (SDF. Format (timeend. gettime () + "<br> ");
  20. // Obtain the current time
  21. Calendar rightnow = calendar. getinstance ();
  22. // Out. Print (SDF. Format (rightnow. gettime () + "<br> ");
  23. Long Now = rightnow. gettimeinmillis ();
  24. // Calculate the remaining time
  25. Int left = (INT) (end-now );
  26. If (left <= 0 ){
  27. // Time
  28. Out. Print ("over ");
  29. } Else {
  30. Int lefthour = left/(1000*60*60 );
  31. Left = left % (1000*60*60 );
  32. Int leftminute = left/(1000*60 );
  33. Left = left % (1000*60 );
  34. Int leftsecond = left/(1000 );
  35. Out. Print (lefthour + "Hour" + leftminute + "Minute" + leftsecond + "second ");
  36. }
  37. }
  38. } Finally {
  39. Out. Close ();
  40. }
  41. HTML is handed over to jquery. The Code is as follows:
  42. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
  43. <HTML>
  44. <Head>
  45. <Title> </title>
  46. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  47. <SCRIPT src = "../JS/jquery. js" type = "text/JavaScript"> </SCRIPT>
  48. <SCRIPT type = "text/JavaScript">
  49. $ (Document). Ready (function (){
  50. Setinterval (FIG, 1000 );
  51. });
  52. // Obtain the remaining time
  53. Function getlefttime (){
  54. $. Get ("/jquery/timeservlet? ", Function (data ){
  55. If (Data = "over "){
  56. // Window. Location = ".../index. jsp ";
  57. $ ("# Paperform"). Submit ();
  58. }
  59. Else {
  60. $ ("# Nowis"). Text (data );
  61. }
  62. });
  63. }
  64. </SCRIPT>
  65. </Head>
  66. <Body>
  67. <Div id = "nowis"> </div>
  68. <Form ID = "paperform" method = "Post" Action = "/jquery/timeservlet? Action = submit ">
  69. </Form>
  70. </Body>
  71. </Html>
  72. Test.
  73. Alas, the name of jquery is a javascript library. Many people are talking about it and it becomes a technical name. Haha.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.