使用Javascript的在线测验计时器

时间:2013-08-25 09:48:17

标签: 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";
    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();
        }
    }
}

上面的代码运行良好,即使计时器到期,考试也会自动提交。但是,我试图在主体完全加载后调用setTimeOut()和setInterval()方法,即setInterval("calculate_time()",100); // Start clock. setTimeOut("submitForm()", <?php echo $time_limit; ?>);应该在startTimer()中。

    <body onload="StartTimer()">
    ......
    <script>
    .....
    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";
    var ct = setInterval("calculate_time()",100); // Start clock.
setTimeOut("submitForm()", <?php echo $time_limit; ?>);
}
.....
    </script>

但是当我这样做时,我无法完全执行代码。即使计时器到达0:00,也无法提交考试,而是负面计时器正在运行!!请帮帮我!!

2 个答案:

答案 0 :(得分:2)

问题是第二个代码中变量 ct 的范围不正确。实际上,您应该将此变量放在可用于* calculate_time * function。

的上下文中

例如,您可以尝试将变量 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>

找到这类问题根源的最佳方法是使用像FireBug这样的javascript调试程序,它可以轻松查明问题的根源。

答案 1 :(得分:0)

我参与了一个在线测验应用程序,我必须实现计时器。 我创建了一个我在body load上调用的Javascript函数

`< 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设置,它保持会话中的考试时间。

此处提供了具有定时器功能的完整应用程序

http://www.edureka.co/blog/creating-an-online-quiz-application-implementing-countdown-timer/