[英]Countdown timer rstarts from 24hrs when it reaches zero
I developed a computer based testing system where students can test their knowledge with countdown timer. 我开发了基于计算机的测试系统,学生可以在其中使用倒数计时器测试他们的知识。 I used the following codes which relate to the server very well. 我使用了以下与服务器相关的代码。
first_page.php first_page.php
<?php
session_start();
$get_time="select * from tests";
$run_time = mysqli_query($con, $get_time);
$row_time=mysqli_fetch_array($run_time);
$duration=$row_time['duration'];
$_SESSION["duration"]=$duration;
$_SESSION["start_time"]=date("Y-m-d H:i:s");
$end_time=date('Y-m-d H:i:s', strtotime('+'.$_SESSION['duration'].'minutes', strtotime($_SESSION['start_time'])));
$_SESSION['end_time']=$end_time
?>
<script type="text/javascript">
window.location="second_page.php";
</script>
second_page.php second_page.php
<div id="response"></div>
<script type="text/javascript">
setInterval(function(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","response.php",false);
xmlhttp.send(null);
document.getElementById("response").innerHTML=xmlhttp.responseText;
},1000);
</script>
response.php response.php
session_start();
$from_time1=date('Y-m-d H:i:s');
$to_time1=$_SESSION['end_time'];
$timefirst=strtotime($from_time1);
$timesecond=strtotime($to_time1);
$differenceinseconds=$timesecond-$timefirst;
echo gmdate("H:i:s",$differenceinseconds);
The problem now is that when the timer gets to zero, it goes to 24hrs and start counting down. 现在的问题是,当计时器归零时,它将达到24小时并开始递减计数。 Is there a way to amend these codes so that when the timer gets to zero it stops and redirects the user to a different page where I can say “You have used up your time and your answers have been submitted automatically”? 有没有一种方法可以修改这些代码,以便在计时器归零时将其停止并将用户重定向到另一个页面,在该页面上我可以说“您已经用完时间并且答案已自动提交”?
You could do a check in your response callback, something like this: 您可以在响应回调中进行检查,如下所示:
if (xmlhttp !== '0 minutes') {
document.getElementById("response").innerHTML=xmlhttp.responseText;
} else {
window.location = 'put your url here';
}
When looking at your code i saw you do a request every 1 second. 在查看您的代码时,我看到您每1秒钟执行一次请求。 That is really bad practise, the server load with multiple clients will be enormous. 这确实是很糟糕的做法,多个客户端的服务器负载将是巨大的。 Cant you return a date with PHP and do the countdown in javascript? 不能用PHP返回日期并使用JavaScript倒数计时吗? And re-check every 5 minutes if javascript is still in sync with the date from php? 并每5分钟重新检查javascript是否仍与php中的日期同步? from (60 * 60 * 24) request you go to (20 * 24) request per day per client. 从(60 * 60 * 24)请求开始,每天每位客户转到(20 * 24)请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.