Javascript 在线考试网站中的jquery倒计时。自动提交,时间已缩短,浏览器关闭,页面刷新,然后单击“上一步”

Javascript 在线考试网站中的jquery倒计时。自动提交,时间已缩短,浏览器关闭,页面刷新,然后单击“上一步”,javascript,jquery,timer,Javascript,Jquery,Timer,我在我的在线考试网站上使用了“从时间设置到小时和分钟倒计时到零” 学生需要在给定的时间段内出现并完成考试,比如说n分钟后 1> n分钟后,页面必须自动提交 2> n分钟前,如果学生关闭浏览器或单击“上一步”或刷新页面,则应出现确认框 i> 如果是,则必须提交页面。但在此期间,必须继续倒计时 如果退出,则学生必须在剩余时间内继续考试 var time_in_sec=parseInt(小时*3600,10)+parseInt(分钟*60,10); 时间(单位:秒)*=1000; $(函数(){ $(

我在我的在线考试网站上使用了“从时间设置到小时和分钟倒计时到零”

学生需要在给定的时间段内出现并完成考试,比如说n分钟后

1> n分钟后,页面必须自动提交

2> n分钟前,如果学生关闭浏览器或单击“上一步”或刷新页面,则应出现确认框

i> 如果是,则必须提交页面。但在此期间,必须继续倒计时

如果退出,则学生必须在剩余时间内继续考试

var time_in_sec=parseInt(小时*3600,10)+parseInt(分钟*60,10);
时间(单位:秒)*=1000;
$(函数(){
$('hm#u timer')。倒计时({
小时:小时,
分钟:分钟,
尺寸:“lg”
});
警告(“完成前不要单击后退或关闭窗口。”);//开始考试前警告
});
设置超时(
函数(){
警报(“您已达到时间限制”);
document.getElementById(“出现考试”).submit();///表单id
},时间单位为秒);
jQuery(窗口).bind('beforeunload',函数(事件){
event.stopPropagation();
event.returnValue=“确认('您真的想退出此考试吗?')”;
returnevent.returnValue;
});
请帮忙
如何在n分钟之前检查所有这些操作(关闭浏览器、页面刷新或单击后退)是否已发生,以及如何在这些操作期间继续倒计时?

我使用php脚本而不是jquery计时器设置会话中的时间,我的问题已解决

在控制器中:

            if(!empty($test_assigned)){
            $test_detail = $this->Test->findById($test_id);
            $total_time = explode(':',$test_detail['Test']['total_time']);
            $data_arr = $test_detail['Test'];
            $data_arr['htime'] = $total_time[0];
            $data_arr['mtime'] = $total_time[1];
            $test_detail = array('Test'=>$data_arr);
            $this->set('test_detail',$test_detail);
            $this->set('list',$this->Question->getQuestionsForTest($test_id));

            if($this->Session->read('timer_count') != '') {
                // session variable_exists, use that
                $targetDate = $this->Session->read('timer_count');
            } else {//comes here with 1st page load then for each next refresh goes to if
                // No session variable, red from mysql
                $hours = $data_arr['htime']; // Enter hours
                $minutes = $data_arr['mtime']; // Enter minutes
                $seconds = 0; // Enter seconds
                //$time_limit = ($hours * 60 * 60) + ($minutes * 60) + $seconds + 1; // Convert total time into seconds
                $seconds += 1; 
                $targetDate = strtotime("$hours hours $minutes minutes $seconds seconds"); 

                $this->Session->write('timer_count' , $targetDate);
                $this->Session->write('test_id' , $test_id);
            }

            $actualDate = time();
            $secondsDiff = $targetDate - $actualDate;
            $remainingDay     = floor($secondsDiff/60/60/24);
            $remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
            $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24) - ($remainingHour*60*60))/60);
            $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24) - ($remainingHour*60*60))-($remainingMinutes*60));
            $dateFormat = "g:i a";
            $actualDateDisplay = date($dateFormat,$actualDate);
            $targetDateDisplay = date($dateFormat,$targetDate);


            $this->set('remainingHour',$remainingHour);
            $this->set('remainingMinutes',$remainingMinutes);
            $this->set('remainingSeconds',$remainingSeconds);
他认为:

    <?php echo $this->html->css("timer/font/timer.css");?>
<script type="text/javascript">
var hours = <?php echo $remainingHour; ?>  
var minutes = <?php echo $remainingMinutes; ?>  
var seconds = <?php echo $remainingSeconds; ?> 
//alert("  "+hours+" hr "+minutes+" min    "+seconds+" sec");
function setCountDown (){
    seconds--;
    if (seconds < 0){
        minutes--;
        seconds = 59;
    }
    if (minutes < 0){
        hours--;
        minutes = 59;
    }
    if (hours < 0){
        hours = 23;
    }
    var h1 = hours; if(h1 == 0 || h1 < 10){h1 = '0'+h1;}
    var m1 = minutes;if(m1 == 0 || m1 < 10){m1 = '0'+m1;}
    var s1 = seconds;if(s1 == 0 || s1 < 10){s1 = '0'+s1;}
    document.getElementById("htd").innerHTML = h1;
    document.getElementById("mtd").innerHTML = m1;
    document.getElementById("std").innerHTML = s1;
    document.getElementById("time_elasped").value = h1+':'+m1+':'+s1;
    SD=window.setTimeout( "setCountDown()", 1000 );
    if((s1 == '00' || s1 == '0') && (m1 == '00' || m1 == '0') && (h1 == '00' || h1 == '0')){ 
        seconds = "00"; window.clearTimeout(SD);
        alert("You reached the time limit.");
        document.getElementById("appear_exam").submit();
        //window.location = "result.php"
    } 
}
$(function(){
    setCountDown();
});
</script>
<div style="float:left; margin-left:10px; position:fixed; margin-top:-30px;">
<table class="timer-table">
    <tr >
        <td class="timer-td">Hrs</td>
        <td class="timer-td">Mins</td>
        <td class="timer-td">Secs</td>
    </tr>
    <tr>
        <td id="htd" class="timer-td1"><?php echo "$remainingHour";?></td>
        <td id="mtd" class="timer-td1"><?php echo "$remainingMinutes";?></td>
        <td id="std" class="timer-td1"><?php echo "$remainingSeconds";?></td>
    </tr>
</table>
</div>

var小时=
var分钟=
var秒=
//警报(“小时+小时+小时+分钟+分钟+秒+秒”);
函数设置倒计时(){
秒--;
如果(秒<0){
分钟--;
秒=59;
}
如果(分钟<0){
小时--;
分钟=59;
}
如果(小时<0){
小时=23;
}
var h1=小时;如果(h1==0 | | h1<10){h1='0'+h1;}
var m1=分钟;如果(m1==0 | | m1<10){m1='0'+m1;}
变量s1=秒;如果(s1==0 | | s1<10){s1='0'+s1;}
document.getElementById(“htd”).innerHTML=h1;
document.getElementById(“mtd”).innerHTML=m1;
document.getElementById(“std”).innerHTML=s1;
document.getElementById(“time_elasped”).value=h1+':'+m1+':'+s1;
SD=window.setTimeout(“setCountDown()”,1000);
如果((s1='00'| | s1='0')&&(m1='00'| | m1='0')&&(h1='00'| | h1='0'){
秒=“00”窗口。清除超时(SD);
警报(“您已达到时间限制”);
document.getElementById(“出现考试”).submit();
//window.location=“result.php”
} 
}
$(函数(){
设置倒计时();
});
高分辨率分光计
分钟
秒