我正在建立一个从10分钟倒计时的考试模拟器,当它到达00:00时,考试问题被隐藏,并显示一个div说“你我们的时间”,但如果我点击浏览器上的刷新它会回到10:00但我想要它做的是再次显示时间div!我在下面用我的jquery玩了但是我不知道我哪里出错了,计时器存储在一个cookie中,所以如果用户在考试中的任何给定时间按f5,那么当用户按下f5时,cookie会继续倒计时!所以我的问题是,如果时间是00:00,有人可以帮我修改下面的JQuery以显示时间div,并且即使用户刷新浏览器也会显示。
$(function () {
$('.clock').each(function () {
var clock = $(this),
theTime = $.cookie('time'),
seconds = "00", minutes = 10;
if (theTime && theTime != "0:00") {
theTime = theTime.split(":")
minutes = theTime[0]
seconds = theTime[1]
}
$('.min', clock).text(minutes)
$('.sec', clock).text(seconds)
var timer = setInterval(function () {
var m = $('.min', clock), minutes = +m.text(),
s = $('.sec', clock), seconds = -1 + +s.text()
if (seconds < 0) {
seconds = 59
minutes--
} else if (minutes <= 0 && seconds <= 0) {
clock.html('Timer Complete.');
clearInterval(timer);
$.cookie('time', '', { expires: -1 })
$('#feedbackform').hide();
$('#MainContent_DivOutOfTime').toggle();
return;
}
seconds = seconds < 10 ? '0' + seconds : seconds
m.text(minutes)
s.text(seconds)
$.cookie('time', minutes + ":" + seconds, { expires: -1 })
},
1000);
});
});
$(function () {
$('.clock').each(function () {
var clock = $(this),
theTime = $.cookie('time'),
seconds = "00", minutes = 10;
if (theTime && theTime != "0:00") {
theTime = theTime.split(":")
minutes = theTime[0]
seconds = theTime[1]
}
$('.min', clock).text(minutes)
$('.sec', clock).text(seconds)
var timer = setInterval(function () {
var m = $('.min', clock), minutes = +m.text(),
s = $('.sec', clock), seconds = -1 + +s.text()
if (seconds < 0) {
seconds = 59
minutes--
} else if (minutes <= 0 && seconds <= 0) {
clock.html('Timer Complete.');
clearInterval(timer);
$.cookie('time', '', { expires: -1 })
$('#feedbackform').hide();
$('#MainContent_DivOutOfTime').toggle();
return;
}
seconds = seconds < 10 ? '0' + seconds : seconds
m.text(minutes)
s.text(seconds)
$.cookie('time', minutes + ":" + seconds, { expires: -1 })
},
1000);
});
});
我尝试从代码中删除以下内容
if (seconds < 0) {
seconds = 59
minutes--
} else
这样它会检查时间,然后显示div但无济于事!我也删除了clearInterval ...任何帮助都将受到高度赞赏
答案 0 :(得分:0)
这行不清除cookie吗?尝试删除它。
$.cookie('time', '', { expires: -1 })