如何设置倒数计时器在00:00:00停止或在= 0自动点击按钮时停止

时间:2019-05-03 04:42:38

标签: javascript php html timer

当我的计时器转到00:00:00时,下一秒将像23:59:59一样继续倒计时。 如何在00:00:00停止计时器或在计时器= 00:00:00自动点击按钮时停止计时

按钮

<input type=submit name=submit value='Get Result' id='submitpaper'>

显示

<div id=response class=timer style=font-size:30px></div>

这是First.php

$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;
echo gmdate("H:i:s",$differenceinseconds);

?>

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;
}

1 个答案:

答案 0 :(得分:1)

您可以将响应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 countdown smaller than 0, countdown should be 0
if($differenceinseconds<0)$differenceinseconds=0;

echo gmdate("H:i:s",$differenceinseconds);

?>