JS倒計時、跳轉模組以及JSP中比較日期跳轉模組

來源:互聯網
上載者:User

1,前言

       這個是根據網上一些資料重新編寫整理而來的,主要是作為自己使用的一個小積累,原本的目的是使網頁在規定的時間段內自動跳轉,在時間段外的請求不做處理;分別寫了JS和JSP的代碼,放在這裡以備查用;

2,代碼

       2.1 倒計時模組

              這個每隔1秒自動重新整理,可以稍作修改以作他用:               

<!--倒計時-->  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script language="JavaScript">  function Dclock()  {   var symbol="3月31日";var deadline= new Date("03/31/2013 11:25:00"); //標準時間格式var now = new Date();var diff = -480 - now.getTimezoneOffset(); //是北京時間和當地時間的時間差  var leave = (deadline.getTime() - now.getTime()) + diff*60000; //getTime()可以取得1970之後的毫秒數,1970年前為負數var day = Math.floor(leave / (1000 * 60 * 60 * 24));  //天數var hour = Math.floor(leave / (1000*3600)) - (day * 24);  var minute = Math.floor(leave / (1000*60)) - (day * 24 *60) - (hour * 60);  var second = Math.floor(leave / (1000)) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);  if (now.getTime()<deadline.getTime()){LiveClock.innerHTML = "現在離"+symbol+"還有"+day+"天"+hour+"小時"+minute+"分"+second +"秒"  ;test.innerHTML= "getTime:"+now.getTime()+"Deadline:"+deadline.getTime();setTimeout("Dclock()",1000); //調用自身每隔1秒 }  else if (now.getTime()>deadline.getTime()){LiveClock.innerHTML = "END";setTimeout("Dclock()",1000);}}  </script>  <!--倒計時-->  <body onload=Dclock()>  <div id= LiveClock></div>  <div id=test></div></body>

         2.2 JS跳轉

<!--Javascript begin-->  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script language="JavaScript">  function Dclock()  {   var deadline= new Date("05/31/2012 20:30:00"); //記時起點var deadline_2= new Date("05/31/2012 21:30:00"); //記時結束    var symbol="5月31日";var now = new Date();if (now.getTime()<deadline.getTime()){}  else if (deadline.getTime()<now.getTime()&&now.getTime()<deadline_2.getTime())//C語言寫慣了a<b<c了把{//alert(now.getTime());window.location.href="http://localhost/XXX/page.html";setTimeout("Dclock()",1000);}else if (now.getTime()>deadline_2.getTime()){}}  </script>  <Javascript end-->  

         2.3 JSP代碼

              由於JS取得是用戶端時間,雖然可以通過互動來取得伺服器時間,但是顯得有些多餘,這裡貼一下JSP的代碼

<%@ page contentType="text/html; charset=UTF-8" import="java.util.*;" %><%//擷取目前時間java.util.Date now=new java.util.Date();java.util.Date dline=new java.util.Date("03/01/2012 20:30:00");java.util.Date dline2=new java.util.Date("03/03/2012 21:30:00");if (now.getTime()<dline.getTime()){}  else if (dline.getTime()<now.getTime()&&now.getTime()<dline2.getTime())//C語言寫慣了a<b<c了把{response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);String newLocn="http://XXX/page/earthhour/";response.setHeader("Location",newLocn);}else if (now.getTime()>dline2.getTime()){}%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.