Project Structure:
Running effect:
========================================================== ========================
CodePart:
========================================================== ========================
/Clock/webcontent/index. jsp
1 <% @ Page Language = " Java " Contenttype = " Text/html; charsets = UTF-8 " Pageencoding = " UTF-8 " %> 2 <% 3 String Path = Request. getcontextpath (); 4 String Basepath = Request. getscheme () + " :// " 5 + Request. getservername () + " : " + Request. getserverport () 6 + Path + " / " ; 7 %> 8 <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd" > 9 < Html > 10 < Head > 11 < Base Href = "<% = Basepath %>" /> 12 < Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" > 13 < Title > Insert title here </ Title > 14 </ Head > 15 < Body > 16 < Span ID = "Clock" > </ Span > 17 </ Body > 18 < Script Type = "Text/JavaScript" SRC = "<% = Basepath %> JS/clock. js" Charset = "UTF-8" > </ Script > 19 </ Html >
/Clock/webcontent/JS/clock. js
1 /* * 2 * Time Display 3 * @ Date 2012-12-31 4 * @ Author hongten (hongtenzone@foxmail.com) 5 * 6 * @ Returns 7 */ 8 Function Clock (){ 9 VaR Date = New Date (); 10 This . Year = Date. getfullyear (); 11 This . Month = date. getmonth () + 1 ; 12 This . Date = Date. getdate (); 13 This . Day = New Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ) [Date. getday ()]; 14 This . Hour = date. gethours () <10? "0" + Date. gethours (): Date. gethours (); 15 This . Minute = date. getminutes () <10? "0" +Date. getminutes (): Date. getminutes (); 16 This . Second = date. getseconds () <10? "0" + Date. getseconds (): Date. getseconds (); 17 18 // Date: 2012-12-31-17:03:18 Monday 19 This . Tostring = Function (){ 20 Return "Date:" +This . Year + "-" + This . Month + "-" + This . Date + "-" + This . Hour + ":" + This . Minute + ":" + This . Second + "" + This . Day; 21 }; 22 23 // 2012-12-31 24 This . Tosimpledate = Function (){ 25 Return This . Year + "-" + This . Month + "-" + This . Date; 26 }; 27 28 // 17:04:03 29 This . Todetaildate = Function (){ 30 Return This . Year + "-" + This . Month + "-" + This . Date + "" + This . Hour + ":" + This . Minute + ":" + This . Second; 31 }; 32 33 This . Display = Function (Ele ){ 34 VaR Clock = New Clock (); 35 Ele. innerhtml = Clock. tostring (); 36 Window. setTimeout ( Function (){ 37 Clock. Display (Ele ); 38 }, 1000 ); 39 }; 40 } 41 42 // <Span id = "Clock"> </span> 43 VaR Clock = New Clock (); 44 Clock. Display (document. getelementbyid ("Clock "));
I personally think this is very practical in the Web... So I recommend it to you...
I'm hongten