我试图在时间(从数据库导入)中显示div - 当前时间将为0.如何做到这一点?这是代码:
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{
echo "<script>";
echo "$(document).ready(function(){";
echo "$('#". $row['id'] . "').show();";
echo "$('#". $row['id'] . "').delay(15000);";
echo "$('#". $row['id'] . "').hide();";
echo "});";
echo "</script>";
}
}
答案 0 :(得分:0)
试一试: -
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{ ?>
<script>
$(document).ready(function(){
$('#<?php echo $row['id'] ?>').show();
$('#<?php echo $row['id'] ?>').delay(15000);
$('#<?php echo $row['id'] ?>').hide();
});
</script>
<?php }
}
?>
答案 1 :(得分:0)
我认为你想在你的javascript代码而不是setTimeout()
PHP方面添加if ($differenceInSeconds==0) {}
。
你还需要重写你的jQuery(有效地显示一个元素,不做任何事情并立即隐藏它)
$("#element").show();
$("#element").delay(15000);
$("#element").hide();
到
$("#element").show().delay(15000).hide(0);
以下完整代码。
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
// Adding a setTimeout so your JS code actually gets written and executes when it should.
if ($differenceInSeconds >= 0) {
echo "<script>";
echo "$(document).ready(function(){";
echo "window.setTimeout(function() {";
echo "$('#". $row['id'] . "').show().delay(15000).hide(0);";
echo "}, " . $differenceInSeconds * 1000 . ");";
echo "});";
echo "</script>";
}
}