求一个JS或AJAX的倒计时,要求时间从数据库里提取
现在再做一个类似拍卖的网页,想要一个淘宝那样的倒计时,记录商品还有多少时间下架。我现在会写倒计时,但是老师要求是从数据库中提出物品时间再做倒计时!!!我是个菜鸟,不怎么会...
现在再做一个类似拍卖的网页,想要一个淘宝那样的倒计时,记录商品还有多少时间下架。
我现在会写倒计时,但是老师要求是从数据库中提出物品时间再做倒计时!!!
我是个菜鸟,不怎么会,请教各位达人有何良策?
数据库中:有上传时间uploadTime和结束时间endDate的值
求达人给个参考代码,万分感谢!!! 展开
我现在会写倒计时,但是老师要求是从数据库中提出物品时间再做倒计时!!!
我是个菜鸟,不怎么会,请教各位达人有何良策?
数据库中:有上传时间uploadTime和结束时间endDate的值
求达人给个参考代码,万分感谢!!! 展开
1个回答
2013-11-11
展开全部
以下是servlet
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
try {
if ("submit".equals(request.getParameter("action"))) {
//交卷
//TODO:这里做交卷处理
out.println("已交卷");
} else {
//倒计时
//设置结束时间
Calendar timeend = Calendar.getInstance();
timeend.set(2008, 10, 7, 14, 50, 0);
long end = timeend.getTimeInMillis();
//out.print(sdf.format(timeend.getTime())+"<br>");
//获取当前时间
Calendar rightNow = Calendar.getInstance();
//out.print(sdf.format(rightNow.getTime())+"<br>");
long now = rightNow.getTimeInMillis();
//计算剩余时间
int left = (int) (end - now);
if (left <= 0) {
//时间到
out.print("over");
} else {
int leftHour = left / (1000 * 60 * 60);
left = left % (1000 * 60 * 60);
int leftMinute = left / (1000 * 60);
left = left % (1000 * 60);
int leftSecond = left / (1000);
out.print(leftHour + "时" + leftMinute + "分" + leftSecond + "秒");
}
}
} finally {
out.close();
}
以下是html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../js/jquery.js" type="text/javascript"></script>
<script type="text/JavaScript">
$(document).ready(function(){
setInterval(getLeftTime,1000);
});
//得到剩余时间
function getLeftTime(){
$.get("/jquery/TimeServlet?",function(data){
if(data=="over"){
//window.location="../index.jsp";
$("#paperform").submit();
}
else{
$("#nowis").text(data);
}
});
}
</script>
</head>
<body>
<div id="nowis" ></div>
<form id="paperform" method="POST" action="/jquery/TimeServlet?action=submit">
</form>
</body>
</html>
其实你说的只是在servlet中处理时间,无非是拿到数据库中时间的字段,然后++--,然后set 然后就OK ,不懂继续问
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
try {
if ("submit".equals(request.getParameter("action"))) {
//交卷
//TODO:这里做交卷处理
out.println("已交卷");
} else {
//倒计时
//设置结束时间
Calendar timeend = Calendar.getInstance();
timeend.set(2008, 10, 7, 14, 50, 0);
long end = timeend.getTimeInMillis();
//out.print(sdf.format(timeend.getTime())+"<br>");
//获取当前时间
Calendar rightNow = Calendar.getInstance();
//out.print(sdf.format(rightNow.getTime())+"<br>");
long now = rightNow.getTimeInMillis();
//计算剩余时间
int left = (int) (end - now);
if (left <= 0) {
//时间到
out.print("over");
} else {
int leftHour = left / (1000 * 60 * 60);
left = left % (1000 * 60 * 60);
int leftMinute = left / (1000 * 60);
left = left % (1000 * 60);
int leftSecond = left / (1000);
out.print(leftHour + "时" + leftMinute + "分" + leftSecond + "秒");
}
}
} finally {
out.close();
}
以下是html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="../js/jquery.js" type="text/javascript"></script>
<script type="text/JavaScript">
$(document).ready(function(){
setInterval(getLeftTime,1000);
});
//得到剩余时间
function getLeftTime(){
$.get("/jquery/TimeServlet?",function(data){
if(data=="over"){
//window.location="../index.jsp";
$("#paperform").submit();
}
else{
$("#nowis").text(data);
}
});
}
</script>
</head>
<body>
<div id="nowis" ></div>
<form id="paperform" method="POST" action="/jquery/TimeServlet?action=submit">
</form>
</body>
</html>
其实你说的只是在servlet中处理时间,无非是拿到数据库中时间的字段,然后++--,然后set 然后就OK ,不懂继续问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询