Javascript 使用Java脚本的在线测验计时器
我正在编写一个在线定时考试,为了让计时器运行,我正在使用JS!计时器启动、计算时间以及在计时器过期时提交考试的代码如下:Javascript 使用Java脚本的在线测验计时器,javascript,timer,Javascript,Timer,我正在编写一个在线定时考试,为了让计时器运行,我正在使用JS!计时器启动、计算时间以及在计时器过期时提交考试的代码如下: <script> //Once the complete page is loaded the GIF image disappears and questions are displayed function StartTimer() { document.getElementById('Loading').style.display = "none
<script>
//Once the complete page is loaded the GIF image disappears and questions are displayed
function StartTimer()
{
document.getElementById('Loading').style.display = "none";
document.getElementById('Loaded').style.display = "block";
document.getElementById('1').style.display = "block";
document.getElementById('qustn1').style.backgroundColor="#dd6e23";
}
//Sets the Interval to the time
var ct = setInterval("calculate_time()",100); // Start clock.
setTimeOut("submitForm()", <?php echo $time_limit; ?>);
function submitForm()
{
document.getElementById("submit").submit();
}
function calculate_time()
{
var end_time = "<?php echo $_SESSION["start_time"]; ?>"; // Get end time from session variable (total time in seconds).
var dt = new Date(); // Create date object.
var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
var secs = total_time - (mins * 60); // Extract remainder seconds if any.
if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.
// Check for end of time, stop clock and display message.
if(mins <= 0)
{
if(secs <= 0 || mins < 0)
{
clearInterval(ct);
document.getElementById("txt").value = "0:00";
submitForm();
}
}
}
//加载完整页面后,GIF图像将消失并显示问题
函数StartTimer()
{
document.getElementById('Loading').style.display=“无”;
document.getElementById('Loaded').style.display=“block”;
document.getElementById('1').style.display=“block”;
document.getElementById('qustn1').style.backgroundColor=“#dd6e23”;
}
//将间隔设置为时间
var ct=setInterval(“计算时间()”,100);//开始计时。
setTimeOut(“submitForm()”,);
函数submitForm()
{
document.getElementById(“提交”).submit();
}
函数计算时间()
{
var end_time=”“//从会话变量获取结束时间(总时间,以秒为单位)。
var dt=new Date();//创建日期对象。
var time_stamp=dt.getTime()/1000;//获取当前分钟数(转换为秒)。
var total_time=end_time-Math.round(time_stamp);//从总秒数中减去当前秒数,得到剩余秒数。
var mins=Math.floor(total_time/60);//从剩余秒数中提取分钟数。
var secs=总时间-(分钟*60);//提取剩余秒数(如果有)。
如果(secs<10){secs=“0”+secs;}//检查秒数是否小于10,并在前面添加一个0。
document.getElementById(“txt”).value=mins+:“+secs;//显示剩余的分钟和秒数。
//检查时间结束、停止时钟和显示消息。
若有(分钟),;
}
.....
但当我这样做时,我无法准确地执行代码。即使计时器达到0:00,也无法提交考试,相反,负计时器正在运行!!请帮帮我 问题在于第二个代码中变量ct的范围不正确。实际上,您应该将这个变量放在一个可用于*calculate_time*函数的上下文中 例如,您可以尝试以下代码,将变量ct移动到最外面的范围:
<body onload="StartTimer()">
......
<script>
.....
var ct = null;
function StartTimer() {
document.getElementById('Loading').style.display = "none";
document.getElementById('Loaded').style.display = "block";
document.getElementById('1').style.display = "block";
document.getElementById('qustn1').style.backgroundColor="#dd6e23";
ct = setInterval("calculate_time()",100); // Start clock.
setTimeOut("submitForm()", <?php echo $time_limit; ?>);
}
function submitForm() {
document.getElementById("submit").submit();
}
function calculate_time() {
var end_time = "<?php echo $_SESSION["start_time"]; ?>"; // Get end time from session variable (total time in seconds).
var dt = new Date(); // Create date object.
var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
var secs = total_time - (mins * 60); // Extract remainder seconds if any.
if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.
// Check for end of time, stop clock and display message.
if(mins <= 0) {
if(secs <= 0 || mins < 0) {
clearInterval(ct);
document.getElementById("txt").value = "0:00";
submitForm();
}
}
}
.....
</script>
......
</body>
......
.....
var-ct=null;
函数StartTimer(){
document.getElementById('Loading').style.display=“无”;
document.getElementById('Loaded').style.display=“block”;
document.getElementById('1').style.display=“block”;
document.getElementById('qustn1').style.backgroundColor=“#dd6e23”;
ct=setInterval(“计算时间()”,100);//开始时钟。
setTimeOut(“submitForm()”,);
}
函数submitForm(){
document.getElementById(“提交”).submit();
}
函数计算时间(){
var end_time=”“//从会话变量获取结束时间(总时间,以秒为单位)。
var dt=new Date();//创建日期对象。
var time_stamp=dt.getTime()/1000;//获取当前分钟数(转换为秒)。
var total_time=end_time-Math.round(time_stamp);//从总秒数中减去当前秒数,得到剩余秒数。
var mins=Math.floor(total_time/60);//从剩余秒数中提取分钟数。
var secs=总时间-(分钟*60);//提取剩余秒数(如果有)。
如果(secs<10){secs=“0”+secs;}//检查秒数是否小于10,并在前面添加一个0。
document.getElementById(“txt”).value=mins+:“+secs;//显示剩余的分钟和秒数。
//检查时间结束、停止时钟和显示消息。
如果(分钟我在一个在线测验应用程序上工作,我必须实现计时器。
我创建了一个Javascript函数,在body-load时调用它
`< script language ="javascript" >
var tim;
var min = '${sessionScope.min}';
var sec = '${sessionScope.sec}';
function customSubmit(someValue){
document.questionForm.minute.value = min;
document.questionForm.second.value = sec;
document.questionForm.submit();
}
function examTimer() {
if (parseInt(sec) >0) {
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
sec = parseInt(sec) - 1;
tim = setTimeout("examTimer()", 1000);
}
else {
if (parseInt(min)==0 && parseInt(sec)==0){
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
alert("Time Up");
document.questionForm.minute.value=0;
document.questionForm.second.value=0;
document.questionForm.submit();
}
if (parseInt(sec) == 0) {
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
min = parseInt(min) - 1;
sec=59;
tim = setTimeout("examTimer()", 1000);
}
}
}
< /script>
`
`
变量min和sec的值由sessionVariable设置,sessionVariable在会话中保存考试时间
这里提供了具有计时器功能的完整应用程序
谢谢@Mohammad Dashti
`< script language ="javascript" >
var tim;
var min = '${sessionScope.min}';
var sec = '${sessionScope.sec}';
function customSubmit(someValue){
document.questionForm.minute.value = min;
document.questionForm.second.value = sec;
document.questionForm.submit();
}
function examTimer() {
if (parseInt(sec) >0) {
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
sec = parseInt(sec) - 1;
tim = setTimeout("examTimer()", 1000);
}
else {
if (parseInt(min)==0 && parseInt(sec)==0){
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
alert("Time Up");
document.questionForm.minute.value=0;
document.questionForm.second.value=0;
document.questionForm.submit();
}
if (parseInt(sec) == 0) {
document.getElementById("showtime").innerHTML = "Time Remaining :"+min+" Minutes ," + sec+" Seconds";
min = parseInt(min) - 1;
sec=59;
tim = setTimeout("examTimer()", 1000);
}
}
}
< /script>