求助页面刷新,js计时器不刷新的方法

心中自然无码 2015-06-16 05:16:27
_obj.timeCounter = (function() {
var total = examTimeLength*60; // 单位秒
return function(elemId) {
obj = document.getElementById(elemId);
var s = (total % 60) < 10 ? ('0' + total % 60) : total % 60;
var h = total / 3600 < 10 ? ('0' + parseInt(total / 3600))
: parseInt(total / 3600);
var m = (total - h * 3600) / 60 < 10 ? ('0' + parseInt((total - h * 3600) / 60))
: parseInt((total - h * 3600) / 60);
obj.innerHTML = h + ' : ' + m + ' : ' + s;
total--;
timerConverse = setTimeout("T.timeCounter('" + elemId + "')", 1000);
window.name = total;
if (total < 0){
clearTimeout(timerConverse);
alert('考试时间已到!您无法继续答卷,点击确定后提交试卷。');
T.resultDetailSave(true);
}
}
})();

这是js代码
<#if !isView>
<div class="myexa-timer" style="top:104px;right:60px;">
<p id="timeCounter" style="font-size:18px;"></p>
<button type="button" class="myexa-timer-btn">查看考试进度</button>
</div>
</#if>

这是页面的,想实现刷新页面后,计时器还是显示刷新前的数据要怎么做,开始自己用了window.name。不知道自己写错了,还是不行。求大神指教该如何写!
...全文
759 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Luminous_dxx 2017-01-21
  • 打赏
  • 举报
回复
为什么不用Date()来创建时间戳来判定考试时常,不然就要用cookie或者webstorage来保存记录时间,而且页面关掉了,计时器就不记时了。
  • 打赏
  • 举报
回复
求代码大仙595491721@qq.com
scscms太阳光 2015-06-16
  • 打赏
  • 举报
回复
cookie都不行。换个浏览器、或者清除cookie时间一样也没了。最好是记录进入考场时间,每次刷新取出时间来统计即可。
天际的海浪 2015-06-16
  • 打赏
  • 举报
回复
可以用cookie或者localStorage 保存计时数据啊
心中自然无码 2015-06-16
  • 打赏
  • 举报
回复
我还想请问下,如果我在关闭页面后,再打开,计时器也不还原要如何实现呢?非要到后台和数据库做交互吗?
心中自然无码 2015-06-16
  • 打赏
  • 举报
回复
引用 1 楼 jslang 的回复:

_obj.timeCounter = (function() {
					var total = parseInt(window.name, 10); // 单位秒
					if (!total || total<=0)
						total = examTimeLength*60; // 单位秒
					return function(elemId) {
						obj = document.getElementById(elemId);
						var s = (total % 60) < 10 ? ('0' + total % 60) : total % 60;
						var h = total / 3600 < 10 ? ('0' + parseInt(total / 3600))
								: parseInt(total / 3600);
						var m = (total - h * 3600) / 60 < 10 ? ('0' + parseInt((total - h * 3600) / 60))
								: parseInt((total - h * 3600) / 60);
						obj.innerHTML = h + ' : ' + m + ' : ' + s;
						total--;
						timerConverse = setTimeout("T.timeCounter('" + elemId + "')", 1000);
						window.name = total;
						if (total < 0){
							clearTimeout(timerConverse);
							alert('考试时间已到!您无法继续答卷,点击确定后提交试卷。');
							T.resultDetailSave(true);
						}
					}
				})();

多谢,已解决,虽然不是完全这么写的,但也差不多。
天际的海浪 2015-06-16
  • 打赏
  • 举报
回复

_obj.timeCounter = (function() {
					var total = parseInt(window.name, 10); // 单位秒
					if (!total || total<=0)
						total = examTimeLength*60; // 单位秒
					return function(elemId) {
						obj = document.getElementById(elemId);
						var s = (total % 60) < 10 ? ('0' + total % 60) : total % 60;
						var h = total / 3600 < 10 ? ('0' + parseInt(total / 3600))
								: parseInt(total / 3600);
						var m = (total - h * 3600) / 60 < 10 ? ('0' + parseInt((total - h * 3600) / 60))
								: parseInt((total - h * 3600) / 60);
						obj.innerHTML = h + ' : ' + m + ' : ' + s;
						total--;
						timerConverse = setTimeout("T.timeCounter('" + elemId + "')", 1000);
						window.name = total;
						if (total < 0){
							clearTimeout(timerConverse);
							alert('考试时间已到!您无法继续答卷,点击确定后提交试卷。');
							T.resultDetailSave(true);
						}
					}
				})();

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧