Javascript 变量(时间)为0时显示div

Javascript 变量(时间)为0时显示div,javascript,php,jquery,Javascript,Php,Jquery,我试图在时间(从数据库导入)为0时显示div。怎么做?代码如下: 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>&

我试图在时间(从数据库导入)为0时显示div。怎么做?代码如下:

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>";
    }
}
while($row=mysqli\u fetch\u数组($result)){
回声“

”; echo$row['title']; 回声“

”; echo$行[“类别”]; 回声“




”; echo$row['description']; 回声“; $timeFirst=strottime($row['date']); $timeSecond=strottime(“现在”); $differenceInSeconds=$timeFirst-$timeSecond; 如果($differenceSeconds==0) { 回声“; echo“$(文档).ready(函数(){”; echo“$('#“$行['id']。”).show();”; 回显“$”(“#”。$行['id']。”)。延迟(15000);”; echo“$”(“#”。$行['id'].“).hide();”; 回声“});”; 回声“; } }
试试看:-

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

$(文档).ready(函数(){
$('#')。show();
元(15000元);;
$('#')。隐藏();
});
试试看:-

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

$(文档).ready(函数(){
$('#')。show();
元(15000元);;
$('#')。隐藏();
});

我想您应该在javascript代码中添加一个
setTimeout()
,而不是
if($differenceInSeconds==0){}
PHP端

此外,还需要从重写jQuery(有效地显示元素,不延迟任何内容并立即隐藏它)

完整代码如下

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>";
    }
}
while($row=mysqli\u fetch\u数组($result)){
回声“

”; echo$row['title']; 回声“

”; echo$行[“类别”]; 回声“




”; echo$row['description']; 回声“; $timeFirst=strottime($row['date']); $timeSecond=strottime(“现在”); $differenceInSeconds=$timeFirst-$timeSecond; //添加一个setTimeout,这样您的JS代码就会在应该的时候被写入并执行。 如果($differenceInSeconds>=0){ 回声“; echo“$(文档).ready(函数(){”; echo“window.setTimeout(function(){”; echo“$”(“#”。$行['id'].“).show().delay(15000).hide(0);”; echo“},.$differenceInSeconds*1000.”;“; 回声“});”; 回声“; } }
我想您应该在javascript代码中添加一个
setTimeout()
,而不是
if($differenceInSeconds==0){}
PHP端

此外,还需要从重写jQuery(有效地显示元素,不延迟任何内容并立即隐藏它)

完整代码如下

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>";
    }
}
while($row=mysqli\u fetch\u数组($result)){
回声“

”; echo$row['title']; 回声“

”; echo$行[“类别”]; 回声“




”; echo$row['description']; 回声“; $timeFirst=strottime($row['date']); $timeSecond=strottime(“现在”); $differenceInSeconds=$timeFirst-$timeSecond; //添加一个setTimeout,这样您的JS代码就会在应该的时候被写入并执行。 如果($differenceInSeconds>=0){ 回声“; echo“$(文档).ready(函数(){”; echo“window.setTimeout(function(){”; echo“$”(“#”。$行['id'].“).show().delay(15000).hide(0);”; echo“},.$differenceInSeconds*1000.”;“; 回声“});”; 回声“; } }

$differenceInSeconds=$timeFirst-time()但我想你从来没有看到过这个div:)div在没有显示的情况下看起来正常:无等。我只是觉得在一秒钟的时间间隔内下降相当困难。" ". $秒之前。它们相等吗?
$differenceinconds=$timeFirst-time()但我想你从来没有看到过这个div:)div在没有显示的情况下看起来正常:无等。我只是觉得在一秒钟的时间间隔内下降相当困难。" ". $秒之前。它们相等吗?你应该提供一些你在这个答案中所做的详细信息,而不是仅仅
尝试它
。你应该提供一些你在这个答案中所做的详细信息,而不是仅仅
尝试它
。现在试一试?你能和应该显示的内容进行比较吗?是否存在javascript错误(我不这么认为)?你能把你的HTML源代码发布到pastebin或者别的什么地方吗?我认为它需要检查。不,我需要查看您生成的HTML源代码,而不是服务器端代码。只需按ctrl-U
并将其粘贴到某个位置:-)好的,我发现了问题。您以错误的方式编写了
show()
delay()
hide()
序列-在显示元素之后有效地隐藏了元素。请检查我编辑的答案。哦,哇!它起作用了!非常感谢。但还有另一个问题。当时间为0时,$differenceSeconds为-1、-2等,并且div在时间为-OK时显示alltime,让我更新。你是说带有过去日期的警报吗?现在再试一次?你能和应该显示的内容进行比较吗?是否存在javascript错误(我不这么认为)?你能把你的HTML源代码发布到pastebin或者别的什么地方吗?我认为它需要检查。不,我需要查看您生成的HTML源代码,而不是服务器端代码。只需按ctrl-U
并将其粘贴到某个位置:-)好的,我发现了问题。您以错误的方式编写了
show()
delay()
hide()
序列-在显示元素之后有效地隐藏了元素。请检查我编辑的答案。哦,哇!它起作用了!非常感谢。但还有另一个问题。当时间为0时,$differenceSeconds为-1、-2等,并且div在时间为-OK时显示alltime,让我更新。你是说带有过去日期的警报吗?