当倒数计时器= 0时,发出警报按钮或提交表单

当我的计时器计数为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'>


慕虎7371278
浏览 170回答 1
1回答

慕姐8265434

您的PHP应该只会得到时间上的差异。这意味着PHP将始终输出的格式HH:mm:ss,而不会输出其他文本或值,您可以在JavaScript中获得该格式。除非您使用编码数组,否则确保输出始终是相同的,这意味着您可以设计代码以期望始终发送的值。$differenceinseconds = $timesecond - $timefirst;if ($differenceinseconds < 0){&nbsp; &nbsp; $differenceinseconds = 0;}echo gmdate("H:i:s", $differenceinseconds);然后,您可以在JavaScript中获取值后检查该值,因为现在您知道,您打印的唯一内容response.php是格式为的时间HH:mm:sss。function test() {&nbsp; &nbsp; var xmlhttp = new XMLHttpRequest();&nbsp; &nbsp; xmlhttp.open("GET", "response.php", false);&nbsp; &nbsp; xmlhttp.send(null);&nbsp; &nbsp; var response = xmlhttp.responseText;&nbsp; &nbsp; document.getElementById("response").innerHTML = response;&nbsp; &nbsp; if (response == "00:00:00") {&nbsp; &nbsp; &nbsp; &nbsp; alert("Time's up!");&nbsp; &nbsp; }}如果您也要提交表单,请submit()在条件中添加if (response == "00:00:00") {。document.getElementsByName('myfm')[0].submit();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript