这是倒数计时器的代码
<script language="JavaScript">
<!--
var hoursleft = 0;
var minutesleft = 10; // you can change these values to any value greater than 0
var secondsleft = 0;
var millisecondsleft = 0;
var finishedtext = "Countdown finished!" // text that appears when the countdown reaches 0
end = new Date();
end.setMinutes(end.getMinutes()+minutesleft);
end.setSeconds(end.getSeconds()+secondsleft);
end.setMilliseconds(end.getMilliseconds()+millisecondsleft);
function cd(){
now = new Date();
diff = end - now;
diff = new Date(diff);
var msec = diff.getMilliseconds();
var sec = diff.getSeconds();
var min = diff.getMinutes();
if (min < 10){
min = "0" + min;
}
if (sec < 10){
sec = "0" + sec;
}
if(msec < 10){
msec = "00" +msec;
}
else if(msec < 100){
msec = "0" +msec;
}
if(now >= end){
clearTimeout(timerID);
document.getElementById("cdtime").innerHTML = finishedtext;
}
else{
document.getElementById("cdtime").innerHTML = min + ":" + sec + ":" + msec;
} // you can leave out the + ":" + msec if you want...
// If you do so, you should also change setTimeout to setTimeout("cd()",1000)
timerID = setTimeout("cd()", 10);
}
window.onload = cd;
//-->
</script>
我希望这个计时器能够继续两个php页面,而无需再次重启。当每个问题显示时,计时器不应再次重启。 test1.php页面有所有多项选择问题和test2.php页面都填写了空白问题来回答。在我的应用程序中,首先运行test1.php(即MCQs),并且在所有MCQ之后运行test2.php。如何为两个页面使用相同的计时器。
答案 0 :(得分:0)
我现在在PHP中遇到类似的问题,并制定了解决方案。我不确定以下是否有帮助,但如果这对您有用,请尝试。
将开始时间保存为会话变量 在显示每个问题之前,执行if函数以更新diff作为&#34; end&#34;之间的差异。和&#34;会话[&#34;时间&#34;]&#34;。
if(Session["time"]) {
if(Session["time"]) < end {
diff = end - Session["time"];
} else {
//perform answer submission (may be).
}
} else {
var d = new Date();
Session["time"] = d.getTime();
}