当我的计时器计数为00:00:00
时
alert('Get Result')
按钮或提交表单
我甚至无法在response.php中发出警报消息。
这是我从数据库中选择持续时间的地方
while($row=mysqli_fetch_array($res))
{
$duration=$row["duration"];
}
$_SESSION["duration"]=$duration;
$_SESSION["start_time"]=date("Y-m-d H:i:s");
$end_time=$end_time=date('Y-m-d H:i:s', strtotime('+'.$_SESSION["duration"].'minutes',strtotime($_SESSION["start_time"])));
$_SESSION["end_time"]=$end_time;
这是response.php
<?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;
if($differenceinseconds<0){
// This is for when timer smaller then 0 then = 00:00:00
$differenceinseconds=0;
echo "TIME UP<br>";
//I try to alert a simple message here, and dint work. Why is this happen
}
echo gmdate("H:i:s",$differenceinseconds);
?>
这是测验页面中的javascript
<script type="text/javascript">
var x =setInterval(test,1000);
function test()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","response.php",false);
xmlhttp.send(null);
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
</script>
显示计时器标签
<div id=response class=timer style=font-size:30px></div>
表单名称和按钮
<form name=myfm method=post action=quizz.php>
<input type=submit name=submit value='Get Result'>
答案 0 :(得分:1)
您的PHP应该只获得时间上的差异。这意味着PHP将始终输出HH:mm:ss
的格式,而不会输出其他文本或值,您可以在JavaScript中获得该格式。除非使用编码数组,否则确保输出始终相同,这意味着您可以设计代码以期望始终发送的值。
$differenceinseconds = $timesecond - $timefirst;
if ($differenceinseconds < 0){
$differenceinseconds = 0;
}
echo gmdate("H:i:s", $differenceinseconds);
您可以在JavaScript中获取值之后再检查该值,因为您现在知道response.php
所打印的唯一内容就是时间,格式为HH:mm:sss
。
function test() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "response.php", false);
xmlhttp.send(null);
var response = xmlhttp.responseText;
document.getElementById("response").innerHTML = response;
if (response == "00:00:00") {
alert("Time's up!");
}
}
如果您也要提交表单,请在条件submit()
内添加if (response == "00:00:00") {
。
document.getElementsByName('myfm')[0].submit();