我有 jquery 可以将测验的剩余时间打印到我的页面上。效果很好。现在我想将条件 if else 放入timer_check.php 的输出中。我设置了条件,如果剩余时间 = 0,则回显超时,否则打印剩余时间。我的 jquery 在 quiz.php 页面中。我想检查来自timer_check.php的输出是否超时,然后所有答案都应该提交到数据库,否则在tableHolder div中打印剩余时间。所以我的问题是如何将来自timer_check.php的输出的if else条件放入quiz.php以检查提交测验或打印剩余时间?
$(document).ready(function() {
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('timer_check.php', function(){
setTimeout(refreshTable, 1000);
});
}
timer_check.php
session_start();
include("database.php");
$sql = "select * from ctip_test where test_id = '$_SESSION[testid]'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$starttime = $row['attemp_time'];
}
$datetime1 = new DateTime($starttime);
$datetime1->add(new DateInterval('P0Y0M0DT2H0M0S'));
$currenttime =date('Y-m-d H:i:s');
$datetime2 = new DateTime($currenttime);
$diff = $datetime2->diff($datetime1);
$check_timeout = (array) $datetime2->diff($datetime1);
$hour = $check_timeout['h'];
$minute = $check_timeout['i'];
$second = $check_timeout['s'];
if($hour=="0" && $minute =="0" && $second =="0"){
echo "time out";
}
else{
echo($diff->format("%h hours %i minutes and %s seconds are remaining\n"));
}
最佳答案
您可以尝试使用 ajax 函数,而不是使用加载函数。希望这能给您一些想法,伙计..:)
$(document).ready(function() {
refreshTable();
});
function refreshTable(){
$.ajax({
url: "timer_check.php",
success: function (result) {
if(result == "time out"){
$("#myId").submit(); // myId is the id of the form
}else{
$('#tableHolder').html(result);
setTimeout(refreshTable, 1000);
}
}
});
}
仅供引用
关于javascript - 检查 jquery 中的 if else 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622115/