-2

我遇到了一个代码,据说它只更新放置在标签内的那段div代码。我需要打印剩下的秒数1-1-2013。但我什么也看不见。放置在div标签内的任何东西都是不可见的。由于我无法使用JQueryand Ajax,因此我无法发现我的错误。

<body bgcolor="#73AF59">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">

    </script>

    <audio  preload="auto" src="tones/Time-In-A-Bottle.mp3" loop autoplay> 

    </audio>

    <%! 
      GregorianCalendar newYear =  new GregorianCalendar(2013, 0, 1);  
      /*
       * 0 : January
       * 1 : 1st January
       * 2013 : year
       */           
    %>


<table width="100%">
<tr> 
  <td width="40%"> </td>
  <td> <img src="images/animations/pyear.gif" /> </td>
</tr>

<tr>
  <td width="40%"> <center> <font face="Chiller" size="+6"> <br /> Countdown </font> </center> </td>
  <td>  <center> <font face="Chiller" size="+6"> <br /> 
      <script type="text/javascript">
        var auto_refresh = setInterval(
        function ()
        {
            $('#load_tweets').load('record_count.php').fadeIn("slow");
        }, 1000); // refresh every 10000 milliseconds
        <div id="load_tweets">

         <%= ( newYear.getTimeInMillis() - new GregorianCalendar().getTimeInMillis() ) / 1000 %>

        </div>
      </script>
      </font> </center>
</td>
</tr>

<tr>
  <td colspan="2" style="height:400px"  valign="bottom"> <img src="images/animations/3D_balloons.gif" /> 
        <em>This poor design and animations compiled by Suhail Gupta.</em>
            <audio style="visibility: hidden" > </audio>
  </td>
</tr>
<!-- !-->

声明<%= ( newYear.getTimeInMillis() - new GregorianCalendar().getTimeInMillis() ) / 1000 %>,应该打印 2013 年到来所剩的秒数。

4

2 回答 2

0

有几件事要考虑:

  1. 您缺少doc readyjQuery 的处理程序
  2. 您已将 div 放在<script/>标签内
  3. 尝试删除fadeIn

所以你的代码必须是这样的:

<body bgcolor="#73AF59">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

    <audio  preload="auto" src="tones/Time-In-A-Bottle.mp3" loop autoplay></audio>

    <%! 
      GregorianCalendar newYear =  new GregorianCalendar(2013, 0, 1);  
      /*
       * 0 : January
       * 1 : 1st January
       * 2013 : year
       */           
    %>


<table width="100%">
<tr> 
  <td width="40%"> </td>
  <td> <img src="images/animations/pyear.gif" /> </td>
</tr>

<tr>
  <td width="40%"> <center> <font face="Chiller" size="+6"> <br /> Countdown </font> </center> </td>
  <td>   
      <script type="text/javascript">
      $(document).ready(function(){ // try to put in doc ready handler
        var auto_refresh = setInterval(function (){
            $('#load_tweets').load('record_count.php');
        }, 1000); // refresh every 10000 milliseconds
      });
      </script>
      <center> 
      <font face="Chiller" size="+6"> <br />
        <div id="load_tweets">
         <%= ( newYear.getTimeInMillis() - new GregorianCalendar().getTimeInMillis() ) / 1000 %>
        </div> 
         </font> 
      </center>
</td>
</tr>

<tr>
  <td colspan="2" style="height:400px"  valign="bottom"> <img src="images/animations/3D_balloons.gif" /> 
        <em>This poor design and animations compiled by Suhail Gupta.</em>
            <audio style="visibility: hidden" > </audio>
  </td>
</tr>
<!-- !-->

或者你可以把它移到jQuery下面:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){ // try to put in doc ready handler
        var auto_refresh = setInterval(function (){
            $('#load_tweets').load('record_count.php');
        }, 1000); // refresh every 10000 milliseconds
      });
     </script>

试试这个,看看这是否对你有帮助。

于 2012-12-27T05:29:17.080 回答
0

要以给定的时间间隔在网页上打印结果(减去两个日期的 Mills),您需要创建一个servlet产生 Mills 的减法并在 JSP 中编写 Ajax 例程来请求它servlet

小服务程序


@WebServlet(name = "TestServlet", urlPatterns = {"/test"})
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       double value=((double)(new GregorianCalendar(2013, 0, 1).getTimeInMillis() - new GregorianCalendar().getTimeInMillis()))/1000;
       PrintWriter pw=response.getWriter();
       response.setContentType("text/plain");
       pw.print(value);
       pw.flush();
    }
}

和使用$.ajax方法请求 servlet 的页面:

测试页面.jsp


 <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            setInterval(function(){  
                $.ajax({
                    url: 'test',
                    type: 'get',
                    dataType: 'text',
                    success: function(data){
                        $("#result").html(data);
                    }
                });
            },1000);
        });
    </script>
</head>
<body>
    <div id="result"></div>
</body>
于 2012-12-27T06:04:52.890 回答