日期:2014-05-16  浏览次数:20366 次

动态的在jsp页面中比较当前时间和固定时间

??? ? 用到技术:jquery,所以得有jquerymin.js。

??? ? 业务逻辑:在jsp页面中,有一部分要在一个固定的时间段内显示,出了这个时间段隐藏。

????? 思路:首先有一种情况是客户端打开这个页面后不可能一直在刷新,也不能让整个页面都去刷新。要是不刷新的话,页面上的内容又是静态的,不能自动的去检测当前的时间。所以现在主要是就是让这个页面自动的去检测当前的时间,再和固定时间段的开始时间和结束时间去比较。然后去决定显示与不显示特定的内容。ok直接上代码,代码来的快...<在这说一句,我的上一篇写的有点错误,只是比较了两个时间点,没有考虑到页面不会自动刷新的问题>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
      <title>test</title>
      <!--    导入jquery.js    -->
      <script type="text/javascript" src="jquery132min.js"></script>
      <%
          long current = System.currentTimeMillis();
          java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      %>
      <script type="text/javascript">
          var start=<%=format.parse("2011-10-26 18:58:20").getTime()%>;
          var end = <%=format.parse("2011-10-26 18:58:30").getTime()%>;
          $(document).ready(function() {
                function show(count){
                    window.setTimeout(function(){
                        count+=1000;
                        if(count > start && count<end){
                            document.getElementById("show").style.display='block';
                            show(count);
                        } else{
                            if(count < start) {
                                 show(count);
                            } else{
                            document.getElementById("show").style.display="none";
                            }
                        }
                    },1000);
                }
              show(<%=current%>);
          });


      </script>
  </head>
  <body>
   <div id="show"  style="display:none;">
       <h1>固定时间段显示的内容</h1>
   </div>
  </body>
</html>
?