javascript - 当倒数计时器 = 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'>
解决方案
您的 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();
推荐阅读
- python - 如何清理熊猫数据框中值为“10-12”(以范围表示)的列?
- amazon-dynamodb - 尝试在 dynamo db 中运行 updateitem 时遇到问题
- java - 如何避免流中的空值
- cmake - cmake configure_file 只是为了更新一行而不是覆盖整个文件
- html - 具有角度日程同步融合的不同专业人员的工作日和不同时间
- mongodb - 仅当条件与指定时间范围匹配时,Mongodb 查询才能获取集合
- android - 为 Android 构建 Qt,目标“x86_64-w64-windows-gnu”不支持选项“-fPIC”
- javascript - Bootstrap 4 菜单下拉菜单和子菜单下拉菜单具有相同的高度
- python - 如何解决graphviz中的路径错误?
- python-3.x - 如何将列的值转换为新行