纯jsp中实现倒计时动态显示效果。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<div id="showTimes"></div>

<%
  long current_time=System.currentTimeMillis();
  
  
  long  end_time=1337875200000l;
  
        long time=end_time-current_time;
 %>
<script>
var second = <%= time / 1000%>; // 剩余秒数
// 写一个方法,将秒数专为天数
var toDays = function(){
 var s = second % 60; // 秒
 var mi = (second - s) / 60 % 60; // 分钟
 var h =  ((second - s) / 60 - mi ) / 60 % 24; // 小时
 var d =  (((second - s) / 60 - mi ) / 60 - h ) / 24 // 天
return "剩余:" + d + "天" + h + "小时" + mi + "分钟" + s + "秒";
}
//然后写一个定时器
window.setInterval(function(){
 second --;
 document.getElementById("showTimes").innerHTML = toDays ();
}, 1000);
</script>

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你解答这个问题。 首先,我们可以在 JSP 页面上创建一个表单,让用户输入他们的生日。表单可以包含一个文本框和一个提交按钮,如下所示: ``` <form action="countdown.jsp" method="post"> <label for="birthday">请输入您的生日:</label> <input type="date" id="birthday" name="birthday"> <button type="submit">开始倒计时</button> </form> ``` 在用户提交生日后,我们需要使用 JSP 页面来处理这个请求并计算倒计时。首先,我们需要获取用户输入的生日值: ``` <% String birthday = request.getParameter("birthday"); %> ``` 接下来,我们可以使用 Java 代码来计算距离用户生日还有多少天: ``` <% DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(birthday); Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)); if (cal.getTime().before(Calendar.getInstance().getTime())) { cal.set(Calendar.YEAR, year + 1); } long daysLeft = (cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis()) / (24 * 60 * 60 * 1000); %> ``` 在这个代码段,我们首先使用 SimpleDateFormat 类来将用户输入的生日字符串转换为日期对象。然后,我们可以使用 Calendar 类来获取这个日期对象的年、月和日。接下来,我们需要将年份设置为当前年份,并检查这个日期是否在当前日期之前。如果是,我们将年份设置为下一年,以确保我们计算的是正确的天数。最后,我们使用 getTimeInMillis() 方法来计算距离用户生日还有多少毫,然后将其转换为天数。 最后,我们可以在 JSP 页面上展示倒计时消息: ``` <p>距离您的生日还有 <%= daysLeft %> 天!</p> ``` 完整的 JSP 页面代码如下所示: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>生日倒计时</title> </head> <body> <h1>生日倒计时</h1> <form action="countdown.jsp" method="post"> <label for="birthday">请输入您的生日:</label> <input type="date" id="birthday" name="birthday"> <button type="submit">开始倒计时</button> </form> <% if (request.getParameter("birthday") != null) { String birthday = request.getParameter("birthday"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(birthday); Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)); if (cal.getTime().before(Calendar.getInstance().getTime())) { cal.set(Calendar.YEAR, year + 1); } long daysLeft = (cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis()) / (24 * 60 * 60 * 1000); %> <p>距离您的生日还有 <%= daysLeft %> 天!</p> <% } %> </body> </html> ``` 希望这个例子能够帮助你实现生日倒计时的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值