Я кодирую онлайн-экзамен по таймеру и таймер для запуска Я использую JS! Код для таймера для запуска и рассчитать время и представить экзамен, когда таймер истекает следующим образом:таймер онлайн-таймера с использованием Java Script
<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(), когда тело полностью загружено i.e 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, экзамен не может быть отправлен, вместо этого работает отрицательный таймер! Пожалуйста, помогите мне!!
Благодаря @Mohammad Дашти – user2644795