【问题标题】:Using a stored TimeLapse stored in a datatable inside a php while loop使用存储在 php while 循环内的数据表中的 TimeLapse
【发布时间】:2021-05-30 16:26:58
【问题描述】:

我正在尝试显示数据库中的图像,每个图像之间存在时间延迟。时间延迟可从用户界面中选择 并在几秒钟内存储在与图像相同的记录中。我已经显示了图像,但我需要添加动态时间延迟。

时间延迟存储在名为“TimeLapse”的字段中。下面我尝试使用“TimeLapse as $HoldTime”的内容每次 while 循环显示一个新图像。

第一张图片的 TimeLapse 为“5000”。 图片 2 的 TimeLapse 为“15000”。

但看起来它只对两张图像使用了第一个 TimeLapse 5000。”

任何人都可以看到我如何做到这一点。

PHP 代码

while($row_fb = mysqli_fetch_array($fb)){ ?>
    <?php $url = $ImagePath.''.$row_fb['SignageImageName'];
    $HoldTime = $row_fb['TimeLapse']?>
    <div class="outer">
    <div class="banner-container">

    <div id="wrapper">
    <a><img src="/<?php echo $url;?>" class="responsive"/></a>
    </div>
    </div>
    </div>
    <?php
    }

JAVASCRIPT

(function() {
    var a = $('.outer').children();
    var index = 0;
    run()
  
    function run() {
      a.filter('.active').fadeOut(500).removeClass('active');
      a.eq(index).fadeIn(500).addClass('active');
      index = (index + 1) % a.length;
      setTimeout(run, <?php echo $HoldTime;?>);
    }
  })();

【问题讨论】:

  • @Dharman 嗨。感谢别人的帮助有什么问题,这是礼貌的。
  • 显然,如果您在循环中设置$HoldTime 的值,然后在循环外使用它,它将具有最后一次循环迭代的值。
  • 完全没有必要。我们将 Stack Overflow 视为 Wikipedia。任何这样的绒毛都会分散实际内容的注意力
  • @miken32 嗨,你的权利。如果我将 java 脚本放在循环中,它会弄乱显示。你能就我应该怎么做提供任何建议吗?

标签: javascript php jquery settimeout


【解决方案1】:

我会将延迟时间放入 HTML data- attribute 并在 JavaScript 中读取以设置延迟。 (注意使用alternative syntaxshort echo tags,这会使您的PHP 代码在与HTML 混合时更具可读性。)

<?php while($row = $fb->fetch_array()): ?>

<div class="outer">
    <div class="banner-container" data-delay-time="<?=$row['TimeLapse']?>">
        <div id="wrapper">
            <a>
                <img src="/<?=$ImagePath . $row['SignageImageName']?>" class="responsive"/>
            </a>
        </div>
    </div>
</div>

<?php endwhile ?>

已清理 JS 以消除对函数外部变量的任何依赖。相反,它只是寻找活动横幅的下一个兄弟,如果没有,则循环到开头。一些测试 HTML 包含在一个可运行的示例中。

(function() {
    $("div.outer div.banner-container").hide();
    run();

    function run() {
        // get a list of all the banners
        var banners = $("div.outer div.banner-container");
        if (!banners.length) {
            return;
        }
        // active is the one with "active" class, or the first one
        var active = banners.filter(".active").first();
        if (!active.length) {
          active = banners.first();
        }
        // get the next one or loop to the beginning again
        var next = active.next("div.banner-container");
        if (!next.length) {
            next = banners.first();
        }
        active.fadeOut(500).removeClass("active");
        next.fadeIn(500).addClass("active");
        // get the delay time from the data-delay-time attribute
        setTimeout(run, next.data("delayTime"));
    }
})();
.active {
    background: blue;
    color: white
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="outer">
  <div class="banner-container" data-delay-time="1000">foo</div>
  <div class="banner-container" data-delay-time="2000">bar</div>
  <div class="banner-container" data-delay-time="6000">baz</div>
  <div class="banner-container" data-delay-time="10000">boo</div>
</div>

【讨论】:

  • 您好,感谢您的代码。如果我使用它并包含 while 语句的结果,它只会显示第一张图像。在源代码中,我可以看到所有三个图像。如果我使用您的“div class-”outer”的示例,它可以正常工作。任何想法为什么?
  • 不知道,它生成的 HTML 与您之前的几乎相同,只是添加了一个属性。
猜你喜欢
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 2017-05-28
  • 2013-08-19
  • 2018-10-04
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
相关资源
最近更新 更多