方法:
<script type="text/javascript">
$(function () {
//默认加载定时器
dtstart = new Date();
setTimeout(function () { TestTime(dtstart, dtstart, "@sumss", 0); });
})
var t = $("#lefttimer");//剩余时间显示标签
//第一次循环
function TestTime(dtstart, dtup, sums, n) {
dt = new Date();
var dates = Math.abs(dt - new Date(dtstart)) / 1000;//当前时间与考试开始时间间隔
var sss = @sumss - dates;//考试剩余总秒数
var ss = parseInt(sss % 60);
var hh = parseInt(sss / 60 / 60 % 24);
var mm = parseInt(sss / 60 % 60);
if (ss < 10 && ss >= 0) {
ss = "0" + ss;
}
if (hh < 10 && hh >= 0) {
hh = "0" + hh;
}
if (mm < 10 && mm >= 0) {
mm = "0" + mm;
}
t.html(hh+ " : " + mm + " : " + ss);
var dt1 = new Date();
setTimeout(function () { TestTimes(dt, dt1, sss); }, 1000);
}
//不在考试期间修改时间时
function TestTimes(dtstart, dtup, sums) {
dt = new Date();
var s = Math.abs(dt - dtup) / 1000;//当前时间和上一次循环的当前时间相差秒数
if (s > 5 || s < -5) {
TestTimes2(dtstart, dtup, sums);
}
else {
var dates = Math.abs(dt - dtstart) / 1000;//当前时间与考试开始时间间隔
var sss = sums - dates;//考试剩余总秒数
var ss = parseInt(sss % 60);
var hh = parseInt(sss / 60 / 60 % 24);
var mm = parseInt(sss / 60 % 60);
if (ss < 10 && ss >= 0) {
ss = "0" + ss;
}
if (hh < 10 && hh >= 0) {
hh = "0" + hh;
}
if (mm < 10 && mm >= 0) {
mm = "0" + mm;
}
t.html(hh + " : " + mm + " : " + ss);
if (hh <= 0 && mm <= 0 && ss <= 0) {
setTimeout(function () { timeisUp(); }, 1000);
}
else {
var dt1 = new Date();
var s = Math.abs(dt - dt1) / 1000;
if (s > 6 || s < -6) {
TestTimes2(dtstart, dtup, sums);
}
setTimeout(function () { TestTimes(dt, dt1, sss); }, 1000);
}
}
}
//在考试期间修改客户端时间
function TestTimes2(dtstart, dtup, sums) {
//获取服务器当前时间
$.post("/Student/Attestation/GetNoew", function (data) {
var dt = new Date();
var dates = Math.abs(new Date(data) - new Date("@startTime")) / 1000;//当前时间与考试开始时间间隔
var sss = @sumss -dates;//考试剩余总秒数
var ss = parseInt(sss % 60);
var hh = parseInt(sss / 60 / 60 % 24);
var mm = parseInt(sss / 60 % 60);
if (ss < 10 && ss>=0) {
ss = "0" + ss;
}
if (hh < 10 && hh >= 0) {
hh = "0" + hh;
}
if (mm < 10 && mm >= 0) {
mm = "0" + mm;
}
t.html(hh + " : " + mm + " : " + ss);
if (hh <= 0 && mm <= 0 && ss <= 0) {
setTimeout(function () { timeisUp(); }, 1000);
}
else {
var dt1 = new Date();
setTimeout(function () { TestTimes(dt, dt1, sss); }, 1000);
}
})
}
</script>