jsp 运用ajax实现---时间倒计时且刷新不重置
来源:互联网 发布:教美术的软件 编辑:程序博客网 时间:2024/05/29 07:49
首先,这个问题有三四中解决方案
可以使用cookie、缓存,window.name 等等实现,这里我先用window.name 实现呢,因为 window.name属性值,在刷新网页的时候是不会改变的,甚至页面加载了其他域名的页面也不会改变。
那window.name 是干什么用的呢?小白 从stackoverflow上摘了一段
My understanding of it is that the window object is persistent throughout the lifetime of a tab, and represents the window that is loading different HTML documents.Each tab contains its own window object, which is why even when you navigate to/from different pages the window object is persistent, whereas if you check on a different tab the window.name will be different.When opening different html pages, most of them do not override the window.name property, and it is completely optional. If nothing else is manipulating it, it will be what you leave it as. Most pages only touch on manipulating the window.document itself.
我对此的理解是,窗口对象在选项卡的整个生命周期中是持久的,并且表示加载不同HTML文档的窗口。 每个选项卡包含自己的窗口对象,这就是为什么即使您浏览到/从不同的页面,窗口对象是持久的,而如果您检查不同的选项卡,window.name将是不同的。 当打开不同的html页面时,大多数页面不会覆盖window.name属性,它是完全可选的。 如果没有其他的东西在操纵它,它将是你留下的。 大多数页面只触摸操作window.document本身。
我的想法是 首先加载页面时,从服务器获取倒计时时间,在jsp页面做倒计时操作,如果发生页面刷新操作,就再次用ajax获取服务器时间,再回到jsp页面进行倒计时操作
首先是jsp代码
<head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Title</title> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" > var maxtime,minutes,seconds,msg; if(window.name==''){ testCountDown(); } else{// maxtime=window.name; testCountDown(); } function testCountDown() { $.ajax({ "method":"post", "url":"user/countdown.action" , "async": "false",//总感觉这里应该用同步,不该用异步。。。你们觉着呢 "data":{ } , "success":function (data) { if (data.seconds){ maxtime = data.seconds; }else { alert("kong"); } } }) } function CountDown(){ var div = $("#timer"); if(maxtime>=0){ minutes=Math.floor(maxtime/60); seconds=Math.floor(maxtime%60); msg="距离倒计时结束还有:"+minutes+"分"+seconds+"秒"; div[0].innerHTML=msg; if(maxtime==5*60){ alert('距离结束还是有五分钟'); } --maxtime; window.name=maxtime; } else{ clearInterval(timer); alert("倒计时已经结束了"); } } timer=setInterval("CountDown()",1000); </script></head><body><div id="timer"></div></body>
controller 代码
@RequestMapping(value = "/countdown",produces = {"application/json;charset=UTF-8"}) @ResponseBody public String countDownTime (){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse("2017-05-26 12:50:12");//这里的时间自己去定义,应该是从数据库中取 } catch (Exception e) { e.printStackTrace(); } Date nowDate = new Date(System.currentTimeMillis()); long seconds = (date.getTime() - nowDate.getTime())/1000; Map<String,Float>map = new HashMap<String,Float>(); map.put("seconds",new Float(seconds)); try { return new ObjectMapper().writeValueAsString(map); } catch (Exception e){ e.printStackTrace(); } return null; }
但是这样写还是有些瑕疵,因为ajax访问服务器有时间延迟,服务器返回也有时间延迟,所以从前台到服务器在返回到前台,是有一定的延迟的,这方面以后在做完善。
阅读全文
1 0
- jsp 运用ajax实现---时间倒计时且刷新不重置
- 倒计时-----刷新页面不重置
- JSP实现时间倒计时
- ajax实现jsp页面的动态刷新时间
- 不使用用AJAX框架实现团购或秒杀的无刷新倒计时方案
- JavaScript实现刷新不重记得倒计时
- vue2.0实现倒计时的插件(时间戳 刷新 跳转 都不影响)
- jsp实现单刷时间显示,刷新显示时间不刷新页面
- 运用Javascript实现时间重置、进行时间加减计算
- GCD实现倒计时且后台运行不影响计数
- 【Jsp】Ajax提交数据不刷新页面
- 运用JSP+ajax实现分类查询功能
- JSP页面时间倒计时
- Jsp ajax 实现无刷新上传文件
- ajax 定时请求刷新 倒计时
- 无刷新倒计时实现
- php+ajax实现页面自动刷新时间
- Ajax 实现不刷新取最新商品
- Python爬取京东评论(多线程+队列+bs4+pymysql)
- MMU-名词解释
- https为什么安全?
- maven缺失ojdbc6解决方案
- Android在代码中获取当前系统的时间并格式化
- jsp 运用ajax实现---时间倒计时且刷新不重置
- 扫地机器人的三大侦测感应系统原理
- 软件开发技术视频教程
- poi操作excel
- 想要清晰的明白(二)CSS 盒模型Block box与Line box
- 为UICollectionViewFlowLayout添加maximumInteritemSpacing
- hive数据倾斜解决方案
- Windows Shell路径处理函数
- Hive的内表和外表的区别